views:

48

answers:

1

Hi Everybody,

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 this is possible, and how I should try to solve this?

Thanks!

+1  A: 

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;}
Jonathan
Thanx, I found out: the AutoGenerateColumns is hidden in the propertes panel in VS 2010. I just added a public property to my custom DataGridView class, which is actually a shortcut to AutoGenerateColumns. That's propably also the reason why I didn't find it myself :)Nr 3 is also a great function I didn't hear of yet. But not nessacary in this case. Thanx!
VeeWee