m using ms access and c#
i fetch the data frm the database which includes sr.no. and store in a data table.
i thn display the contents using a datagridview.
everythin is working fine..
heres the catch
i do not want to display the sr.no...how can i make tht particular column invisible.
although i do not display the sr.no. i still require it during runtime..

Dani AI

Generated

Quickest way to keep a key like sr.no available at runtime but hidden in a WinForms DataGridView is to hide it after binding, or prevent it from ever being generated.

  • Building on privatevoid’s point: if you are autogenerating, hide the column in DataBindingComplete so it stays hidden on every rebind.
// once, e.g., in the form ctor
dataGridView1.AutoGenerateColumns = true;

dataGridView1.DataBindingComplete += (s, e) =>
{
    // column names with punctuation can be tricky; rename if needed
    var dt = (dataGridView1.DataSource as DataTable);
    if (dt?.Columns.Contains("sr.no") == true)
        dt.Columns["sr.no"].ColumnName = "SrNo";

    var srCol = dataGridView1.Columns
        .Cast<DataGridViewColumn>()
        .FirstOrDefault(c => string.Equals(c.DataPropertyName, "SrNo", StringComparison.OrdinalIgnoreCase));
    if (srCol != null) srCol.Visible = false;

    // keep a checkbox at the far right
    var sel = dataGridView1.Columns["Select"];
    if (sel != null)
        sel.DisplayIndex = dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Visible) - 1;
};

// add the unbound checkbox column once; it will be preserved
var selectCol = new DataGridViewCheckBoxColumn
{
    Name = "Select",
    HeaderText = "",
    Width = 28,
    TrueValue = true,
    FalseValue = false
};
dataGridView1.Columns.Add(selectCol);
  • kvprajapati’s suggestion is the most robust for long term: set AutoGenerateColumns = false and add only the columns you want; the hidden key simply is not added to the grid, but remains in the DataTable. (learn.microsoft.com)

  • A note on towerrounder’s MappingType.Hidden tip: ColumnMapping affects DataSet XML serialization; it does not hide columns in a WinForms DataGridView. Prefer Visible=false or explicit columns. (learn.microsoft.com)

  • To keep the checkbox at the extreme right, set its DisplayIndex after binding (or in DataBindingComplete), as shown above. DisplayIndex controls visual order without changing the column’s Index. (learn.microsoft.com)

  • If your checkbox is bound to non-boolean data (e.g., "Y"/"N" from Access), set TrueValue/FalseValue accordingly on the DataGridViewCheckBoxColumn. (learn.microsoft.com)

For completeness, Microsoft’s guidance on hiding columns and removing autogenerated ones is here. (learn.microsoft.com)

Recommended Answers

All 10 Replies

There is a Column.Visible property, you should be able to set that to false. How you do that depends on how you are binding the data to the grid view.

Turn off AutoGenerateColumns property.

Take a look,

......
            dataGridView1.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn c1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn c2= new DataGridViewTextBoxColumn();
            c1.HeaderText = "HeaderText_for_field1";
            c2.HeaderText = "HeaderText_for_field2";
               
            c1.DataPropertyName = "field1";
            c2.DataPropertyName = "field2";

            dataGridView1.Columns.Add(c1);
            dataGridView1.Columns.Add(c2);

            dataGridView1.DataSource = your_data_source;
            .....

Adding to what private void said there is also a mapping type porperty on data tables. Setting it to mappingtype.hidden will do the same.

DataGridView dgv = new DataGridView();
            dgv.Columns["ColumnName"].Visible = false;

            DataTable dt = new DataTable();
            dt.Columns["ColumnName"].ColumnMapping =   MappingType.Hidden;

create bound column and simply set style display:none

thnx a lot guys..

i tried it the adatapost's way

dataGridView1.DataSource = null;

            OleDbDataAdapter clientinfoDataAdapter;
            DataTable clientinfoDataTable = new DataTable();
            OleDbCommandBuilder clientinfoCommandBuilder;

            clientinfoDataAdapter = new OleDbDataAdapter("Select * from customer", Module1.oledbcon);
            clientinfoDataAdapter.Fill(clientinfoDataTable);
            clientinfoCommandBuilder = new OleDbCommandBuilder(clientinfoDataAdapter);

            dataGridView1.DataSource = clientinfoDataTable;

but in the datagridview i can see only blank textboxes

Change the order of statements:

clientinfoCommandBuilder = new OleDbCommandBuilder(clientinfoDataAdapter);
clientinfoDataAdapter.Fill(clientinfoDataTable);

the rows are generated but every column is blank

ok got the error now its working.
now i also want to add a check box column in the datagridview.
however i want it to be the column in the extreme right.
i can simply create a new check box column. but whn i assign a datasource to the datagridview, the checkbox column becomes the column on the extreme left.

ne suggestions?

Thank you everyone. I figured out the latter part as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.