469,352 Members | 2,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Unable to hide columns in datagridview

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

Jan 17 '06 #1
4 21625
You need to do this after the grid is databound for it to work properly.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Stropher" wrote:
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

Jan 17 '06 #2
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Stropher" wrote:
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

Jan 17 '06 #3
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

Jan 18 '06 #4
Peter Bromberg [C# MVP] wrote:
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.
Jan 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J Gao | last post: by
5 posts views Thread by J | last post: by
1 post views Thread by MrNobody | last post: by
2 posts views Thread by Phil Sandler | last post: by
3 posts views Thread by =?Utf-8?B?U3RldmVU?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.