Block columns in DataGridView
Again a c# question I cant't seem to figure out:
I have a datagridview on my form. In Visual Studio design mode, I enter 4 columns: refernce, country, name & city. After a searchform is filled in, I try to refresh the datasource: A custom class fetches all the data from the selected table and fills the datagridview. The problem is: the datagridview now has all the columns of the table, and I only want the 4 columns entered in design mode.
I can put all the other columns on visible = false in design mode, and that works. But I want this datagridview as a custom control. So I only want to show the 4 entered columns, without disabling all the others. The data which is invisible is used to bind to a panel with other Controls like TextBoxes.
Does somebody know if t开发者_Go百科his is possible, and how I should try to solve this?
Thanks!
You have three ways of hide columns.
1.- setting the visible property to false (as ksogor said) and don't create columns in design mode. Note that
GridView1.Columns["ColumnName"].Visible = false;
is more readable and mantenible that
GridView1.Columns[1].Visible = false;
2.- setting AutoGenerateColumns to false and create columns in the designer
3.- in your class, set the attribute [Browsable(false)] in the fields you don't want to show. Don't create columns in design mode.
The third way will hide the column in all the datagrids of your app. I love it.
[Browsable(false)]
public string Something{get;set;}
精彩评论