Unable to hide columns in datagridview

  • Thread starter Thread starter Stropher
  • Start date Start date
S

Stropher

I have the following:

this.dataGridViewBill.DataSource = tblResult;

//hide the following columns
this.dataGridViewBill.Columns[8].Visible = false; //email
this.dataGridViewBill.Columns[9].Visible = false; //mobiltelefon

When I run the programm, the columns are still displayed.
I am developing with Visual C#2005 Express Edition Beta.

Can any give me some tipps of what I'm doing wrong.

Thanks in anticipation
 
Sample working code (Nortwind, "orders"):

SqlConnection cn = new
SqlConnection(@"server=(local);database=northwind;uid=sa;pwd=;");
SqlCommand cmd = new SqlCommand("select * From orders");
cmd.Connection =cn;

SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Columns[0].Visible = false; //OrderID
this.dataGridView1.Columns[1].Visible = false; //CustomerId

--Peter
 
Thanks Peter for your quick reply.
I figured out, that the problem had to do with MDIParent.
What I did was to make sure that the hiding has to take place
immediately after displaying the form containing the datagridview.
Example: Assuming that the control->datagridview is in Form1
In class MDIParent ..._Load(...)
Form1 f1 = new Form1();
f1.MdiParent = this;
f1.show();
f1.datagridview1.Columns[0].Visible = false;

where datagridview1.Datasource = tableResult; //is done in Form1

With this, I was able to hide the columns in question successfully.

Thanks and regards,
Stropher
 
Peter said:
Sample working code (Nortwind, "orders"):

SqlConnection cn = new
SqlConnection(@"server=(local);database=northwind;uid=sa;pwd=;");
SqlCommand cmd = new SqlCommand("select * From orders");
cmd.Connection =cn;

SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Columns[0].Visible = false; //OrderID
this.dataGridView1.Columns[1].Visible = false; //CustomerId

--Peter

I have similar problem.
I am also using mdichildren with datagridview.
And I want to hide first column hidden on each datagridview.

If child has no tab pages (datagridview inside tab page)
datagridview remembers that it has to hide column.
If child has tab pages I have to manully set it again
after Form_Load.

And on one form I have three datagridview's and I'm
unable to hide columns on all three because third
datagridview is dependant on one of previous so
setting it after Form_Load doesn't work.
It works only if I set it after setting data,
which is way after Form_Load.

Also, I noticed that problematic columns are only
ones that contains primary key.
I have plenty of hidden columns that contains data,
but are not part of the primary key in the table which
is set as datasource for datagridview.

This behaviour seems like a bug to me.
 
Back
Top