Hi all,
I am creating a windows form which uses a datagridview control that will contain 5 columns.
These columns are created thru a dataset. What I want to do is,
I want to Hide the 4th column of the datagridview control and also need to hide the fixed column.
is it possible?
i have tried the following:
dgv.DataSource = dsTemp.Tables[0]; dgv.Columns[4].Visible = false;
when i execute these codes, i am getting "Object reference not set" error.
Please help me to achive my task.
Thanks in advance.
Regards, dhana. |
| Dhanasekaran Guruswamy Wednesday, March 18, 2009 9:15 AM |
In Form2, it seems that dgCostSchedule isn't instantiated. Please try to change it like the following:
DataGridView dgCostSchedule = new DataGridView();
If the problem can't be solved or any further problem, please feel free to let me know.
Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't.- Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, April 09, 2009 6:58 AM
-
|
| Bruce.Zhou Monday, March 23, 2009 9:45 AM |
Thanks for your detail code. Please try to define dgCostSchedule as a member of the Form2 class. Like the following:
| publicForm2() |
| { |
| InitializeComponent(); |
| this.Load+=newEventHandler(Form2_Load); |
| } |
| DataGridViewdgCostSchedule; |
| voidForm2_Load(objectsender,EventArgse) |
| { |
| |
| dgCostSchedule=(DataGridView)this.Controls["SomeName"]; |
|
| } |
|
| privatevoidbutton1_Click(objectsender,EventArgse) |
| { |
| dgCostSchedule.Columns[2].Visible=false; |
| } | Currently, dgCostSchedule is a local variable, you can't refer to it in button1_Click event handler. Please let me know, if there are still any problems exist. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, April 09, 2009 6:58 AM
-
|
| Bruce.Zhou Wednesday, March 25, 2009 6:58 AM |
http://msdn.microsoft.com/en-us/library/0c24a0d7.aspx | if(dgv.Columns.count!=0) | | { | | ///Makesureyouhavemorethan5columns | | dgv.Columns[4].Visible=false; | | } |
Thanks,
A.m.a.L |
| A.m.a.L - aditi.com - Think Product Wednesday, March 18, 2009 10:23 AM |
thanks for your reply.
i think you don't understand my problem.
actually the datasource property is set on the datagridview control but there are no columns when
dgv.Columns[4].Visible=false is executed.
but i can see the 4th column in screen.
this is my problem.
dhana. |
| Dhanasekaran Guruswamy Thursday, March 19, 2009 4:57 AM |
also is it possible to hide the fixed column (grey color column)of a datagridview control?
|
| Dhanasekaran Guruswamy Thursday, March 19, 2009 6:19 AM |
Ok try to handle the Column added Event. private void DataGridView1_ColumnAdded(Object sender, DataGridViewColumnEventArgs e) { if(e.Column=="CoulmnNameToHide") dgv.Columns[e.Column].Visible = false; }
Thanks,
A.m.a.L |
| A.m.a.L - aditi.com - Think Product Thursday, March 19, 2009 6:47 AM |
thanks for your reply.
i think you don't understand my problem.
actually the datasource property is set on the datagridview control but there are no columns when
dgv.Columns[4].Visible=false is executed.
but i can see the 4th column in screen.
this is my problem.
dhana. Hi Dhanasekaran, If there are 4 columns in the DataGridView, keep sure the max index of the Column is 3. If it is bigger than 3, the application will throw indexOutOfRange exception. As your second problem, I think you are trying to hide the RowHeaders. You can set the RowHeadersVisible property to false. Like the following: this.dataGridView1.RowHeadersVisible = false; Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. |
| Bruce.Zhou Monday, March 23, 2009 3:57 AM |
Actually there are 7 columns in the datagridview control.
What i have done is,
I am creating a DataGridView control in the runtime. This creation process is handled in the Form1.
I have created an Inherited form "Form2" which uses the "Form1" as the parent form.
I have Assigned a dataset at the time of creation.
dgv.Columns[4].Visible=false;
The above code is called in the Form2_Load() event.
In this case, I am getting the error.
Thanks for the solution to hide the rowheaders.
dhana. |
| Dhanasekaran Guruswamy Monday, March 23, 2009 9:05 AM |
Would you please provide with more code? It would helpful for me to diagnose the problem.
Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. |
| Bruce.Zhou Monday, March 23, 2009 9:17 AM |
public partial class Form2 : Form { void Form1_Load() { DataGridView dgv = new DataGridView(); dgv.Top = 100; dgv.Name = "SomeName"; dgv.Left = Convert.ToInt32(drGrid["DG_LEFT"]); dgv.Width = Convert.ToInt32(drGrid["DG_WIDTH"]); dgv.Height = Convert.ToInt32(drGrid["DG_HEIGHT"]); dgv.EditMode = DataGridViewEditMode.EditProgrammatically; dgv.RowHeadersVisible = false; dgv.Visible = true;
iUI.fn_PopulateGridValues(dgv, "SOME_SELECT_STATEMENT_WITH_10_FIELDS"); this.Controls.Add(dgv); } }
public partial class Form2 : Form1 { void Form2_Load() { DataGridView dgCostSchedule; dgCostSchedule = (DataGridView)this.Controls["SomeName"]; dgCostSchedule.Columns[2].Visible = false; } } |
| Dhanasekaran Guruswamy Monday, March 23, 2009 9:34 AM |
In Form2, it seems that dgCostSchedule isn't instantiated. Please try to change it like the following:
DataGridView dgCostSchedule = new DataGridView();
If the problem can't be solved or any further problem, please feel free to let me know.
Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't.- Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, April 09, 2009 6:58 AM
-
|
| Bruce.Zhou Monday, March 23, 2009 9:45 AM |
thanks for the reply. but still its throwing error.
i wrote the hiding statement in a button_click event and i called it from Form2_Load().
its working. i dont know why?
|
| Dhanasekaran Guruswamy Monday, March 23, 2009 9:58 AM |
Sorry I haven't seen it clear with the code you provided. I found that you were getting the reference of the DataGridView from this.Controls["SomeName"]. Please make sure this.Controls["SomeName"] is not null.
Also, would you please post the change you've made which will help me to analyze the error?
Thank you.
Please mark the replies as answers if they help and unmark if they don't. |
| Bruce.Zhou Monday, March 23, 2009 11:53 AM |
public partial class Form1 : Form { void Form1_Load() { DataGridView dgv = new DataGridView(); dgv.Top = 100; dgv.Name = "SomeName"; dgv.Left = Convert.ToInt32(drGrid["DG_LEFT"]); dgv.Width = Convert.ToInt32(drGrid["DG_WIDTH"]); dgv.Height = Convert.ToInt32(drGrid["DG_HEIGHT"]); dgv.EditMode = DataGridViewEditMode.EditProgrammatically; dgv.RowHeadersVisible = false; dgv.Visible = true;
iUI.fn_PopulateGridValues(dgv, "SOME_SELECT_STATEMENT_WITH_10_FIELDS"); this.Controls.Add(dgv); } }
public partial class Form2 : Form1 { void Form2_Load(Sender,e) { DataGridView dgCostSchedule; dgCostSchedule = (DataGridView)this.Controls["SomeName"]; Button1_Click(Sender,e); } } private void Button1_Click(Sender,e) { dgCostSchedule.Columns[2].Visible = false; }
also "this.Controls["SomeName"]" is not NULL. also it has dataset with the column which i want to hide.
dhana. |
| Dhanasekaran Guruswamy Wednesday, March 25, 2009 6:33 AM |
Thanks for your detail code. Please try to define dgCostSchedule as a member of the Form2 class. Like the following:
| publicForm2() |
| { |
| InitializeComponent(); |
| this.Load+=newEventHandler(Form2_Load); |
| } |
| DataGridViewdgCostSchedule; |
| voidForm2_Load(objectsender,EventArgse) |
| { |
| |
| dgCostSchedule=(DataGridView)this.Controls["SomeName"]; |
|
| } |
|
| privatevoidbutton1_Click(objectsender,EventArgse) |
| { |
| dgCostSchedule.Columns[2].Visible=false; |
| } | Currently, dgCostSchedule is a local variable, you can't refer to it in button1_Click event handler. Please let me know, if there are still any problems exist. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, April 09, 2009 6:58 AM
-
|
| Bruce.Zhou Wednesday, March 25, 2009 6:58 AM |