Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resizing datagrid column width to 0 to hide column

Status
Not open for further replies.

megabyte214

Programmer
Nov 7, 2001
40
US
I have read that the easiest way to hide a datagrid column is to set its width to 0. I am working on a Windows App and have tried several ideas that I have found, but none seem to work. Any help is greatly appreciated.

Code:
//error: "Index was out of range.  Must be non-negative and less than the size of the collection"
customerDataGrid.TableStyles[0].GridColumnStyles[0].Width = 0;

//error:  "System.Windows.Forms.DataGrid' does not contain a definition for 'Columns"
customerDataGrid.Columns(0).Visible = False;
 
Maybe you're using the wrong brackets?

customerDataGrid.Columns[0].Visible = False;

Anne
 
Anne,

I actually did try that, but it didn't compile. I did find a way to set the column widths of the grid and I then set the column that I wanted to hide to 0, but it seems to me there has to be an easier way.
I had to create a TableStyle and then ColumnStyles for each column. Then I added the ColumnStyles to the TableStyle and then finally the TableStyle to the DataGrid.
Thanks for your input Anne!

-Tracey

Code:
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "Customers";
ts.HeaderBackColor = System.Drawing.Color.MidnightBlue;
ts.AlternatingBackColor = System.Drawing.Color.Gray;
ts.HeaderForeColor = System.Drawing.Color.White; 
ts.BackColor = System.Drawing.Color.LightGray;

DataGridColumnStyle customerIDCol = new DataGridTextBoxColumn();
customerIDCol.MappingName = "CustomerID";
customerIDCol.HeaderText="";
customerIDCol.Width = 0;
ts.GridColumnStyles.Add(customerIDCol);

DataGridColumnStyle customerFNCol = new DataGridTextBoxColumn();
customerFNCol.MappingName = "FirstName";
customerFNCol.HeaderText = "First Name";
customerFNCol.Width = 50;
ts.GridColumnStyles.Add(customerFNCol);

DataGridColumnStyle customerLNCol = new DataGridTextBoxColumn();
customerLNCol.MappingName = "LastName";
customerLNCol.HeaderText = "Last Name";
customerLNCol.Width = 50;
ts.GridColumnStyles.Add(customerLNCol);

customerDataGrid.TableStyles.Add(ts);
 
>>customerDataGrid.Columns(0).Visible = false;
That is working for Web forms and not for Windows form.
When you bind the DataGrid to a data source, provide in the data source only the columns you want to be displayed.
obislavu

 
obislavu,

If I eliminate that column, will I still have access to the data in that column? I'll give it a try, thanks!

 
You can fix your dataset/datatable to hide the column. with MappingType.hidden:

Code:
DataSet DS = new DataSet();

DS.Tables["Tbl"].Columns[0].ColumnMapping = MappingType.Hidden;

This will allow you to hide the column, but you will still be able to manipulate the data.

Good luck,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top