Hiding a DataGridView column
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Hiding a DataGridView column

  1. #1
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    217

    Hiding a DataGridView column

    I am successfully displaying a DataTable with two columns as the DataSource in a DataGridView. The second column contains an internal reference code which I need but would like to hide.

    The method given by MSDN http://windowssdk.msdn.microsoft.com...b7c7e5be15.asp seems to exactly apply:
    Code:
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = MyDataSet;
    dataGridView1.Columns[MyColumnHeaderName].Visible = false;
    However the MSDN method fails at the third line in VS2005 because Columns.Count == 0. Does anyone have a workaround for this bug?

  2. #2
    Join Date
    Nov 2004
    Posts
    105

    Re: Hiding a DataGridView column

    do like this. It will work

    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = MyDataSet;
    if(dataGridView1.Columns.count !=0)
    {
    dataGridView1.Columns[MyColumnHeaderName].Visible = false;
    }
    else
    do some thing

  3. #3
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    217

    Re: Hiding a DataGridView column

    That will keep it from throwing the exception, but it still doesn't hide the column since Count == 0 !! Apparently, the AutoGenerateColumns function is the location of the bug, since zero Columns are accessible in the ColumnCollection, yet the DataGridView is showing two columns!

  4. #4
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    217

    Thumbs down Re: Hiding a DataGridView column

    Lacking guru help, I was finally able to track down this Visual Studio bug myself. The bug is that the DataGridView.AutoGenerateColumns is not invoked until the DataGridView.Enter event is fired. Thus, no columns are available to hide until Enter occurs.

    So, using the MSDN documented method to hide a column (see the first post), the only way a column can be hidden is AFTER the DataGridView is shown and AFTER the user has tabbed or clicked into it. Then the columns from the DataSource finally become accessible as DataGridView.Columns[] and can be hidden. How clever is that? NOT!!

  5. #5
    Join Date
    Dec 2006
    Posts
    1

    Re: Hiding a DataGridView column

    I know this is a little late, but I also ran into this problem and solved it this way.

    While you cant access the column before the control is selected you can access the datatable and prevent the column from being displayed by using the DataColumn's 'ColumnMapping' property.

    DataTable t;

    t.Columns[x].ColumnMapping = MappingType.Hidden;

  6. #6
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Hiding a DataGridView column

    Set the dataMember property of datagridview
    Code:
            DataGridView1.AutoGenerateColumns = True;
            DataGridView1.DataSource = ds;
            DataGridView1.DataMember = "TableName";
            //DataGridView1.DataMember = ds.Tables[0].TableName;
    
            DataGridView1.Columns[0].Visible = False;

  7. #7
    Join Date
    Dec 2011
    Posts
    1

    Re: Hiding a DataGridView column

    When DataGridView is being populated during runtime through a datasource, columns are not available until 'DataBinding' is complete. Hence, to hide columns during runtime, we need to write an event handler like as follows:

    Code:
    DataGridView dgv = new DataGridView();
    dgv.DataSource = ds;// ds is a datatable populated by a table adapter or otherwise
     dgv.DataBindingComplete +=new DataGridViewBindingCompleteEventHandler(dgv_DataBindingComplete);
               
    private void dgv_DataBindingComplete(Object sender, DataGridViewBindingCompleteEventArgs e)
            {
                DataGridView dgv = (DataGridView)sender;
               dgv.Columns[2].Visible = false;
            }
    that's it!!!

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Hiding a DataGridView column

    Hopefully, at some point since the OP posted 5 years ago, he found the answer to the problem
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center