-
January 16th, 2006, 05:10 PM
#1
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?
-
January 17th, 2006, 11:17 AM
#2
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
-
January 17th, 2006, 02:08 PM
#3
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!
-
February 1st, 2006, 12:11 PM
#4
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!!
-
December 8th, 2006, 04:21 PM
#5
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;
-
December 9th, 2006, 05:03 PM
#6
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;
-
December 3rd, 2011, 01:46 PM
#7
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!!!
-
December 4th, 2011, 07:43 AM
#8
Re: Hiding a DataGridView column
Hopefully, at some point since the OP posted 5 years ago, he found the answer to the problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|