Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem on datagrid view header check box
 

Problem on datagrid view header check box

Hi, I am using a datagrid view control in that i will import contact details and that contact details such name, address, email and category are displayed in grid view and also have one check box column, i put one checkbox header control. if i click header check box all child check box should get selected, if am click child check box one by one at last my header check box should get selected, if am unselect header check box all child check boxes are unselected. Header check box is selected, i will un select some child check boxes , header check box should get unselected instead of this all my child check box is cleared, i dont know how this occured. please anyone can help me. very urgent for me.

am wiriting code like

  1. private void dataGridContacts_CellContentClick(object sender, DataGridViewCellEventArgs e)
  2. {
  3. try
  4. {
  5. if (e.ColumnIndex == 0)
  6. {
  7. int alltrue = 1;
  8. for (int i = 0; i <= dataGridContacts.Rows.Count; i++)
  9. {
  10. if (Convert.ToBoolean(dataGridContacts.Rows[i].Cells[0].Value) == true)
  11. {
  12. alltrue++;
  13. }
  14. else if (Convert.ToBoolean(dataGridContacts.Rows[i].Cells[0].Value) == false)
  15. {
  16. ((CheckBox)dataGridContacts.Controls["checkBoxColumn"]).Checked = false;
  17. break;
  18. }
  19. }
  20. if (alltrue == dataGridContacts.Rows.Count)
  21. {
  22. ((CheckBox)dataGridContacts.Controls["checkBoxColumn"]).Checked = true;
  23. }
  24. else
  25. {
  26. ((CheckBox)dataGridContacts.Controls["checkBoxColumn"]).Checked = false;
  27. }
  28. //clearCheckBox = false;
  29. }
  1. DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
  2. checkBoxColumn.Name = "IsCheck".Clone().ToString();
  3. checkBoxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  4. checkBoxColumn.HeaderText = "".Clone().ToString();
  5. dataGridContacts.Columns.Insert(0, checkBoxColumn);
  6. dataGridContacts.Columns["IsCheck"].ReadOnly = false;
  7. dataGridContacts.Columns["IsCheck"].Width = 55;
  8. dataGridContacts.Columns["IsCheck"].Visible = false;
  9. CheckBox ckBoxView = new CheckBox();
  10. ckBoxView.Name = "checkBoxColumn";
  11. ckBoxView.Size = new Size(15, 15);
  12. ckBoxView.Location = new Point(23, 5);
  13. ckBoxView.CheckedChanged += new EventHandler(ckBoxView_CheckedChanged);
  14. //Add the CheckBox into the DataGridView
  15. this.dataGridContacts.Controls.Add(ckBoxView);
  16. dataGridContacts.Controls["checkBoxColumn"].Visible = false;
  1. private void ckBoxView_CheckedChanged(object sender, EventArgs e)
  2. {
  3. lblError.Text = string.Empty;
  4. if (clearCheckBox != false)
  5. {
  6. if (((CheckBox)sender).Checked == true)
  7. {
  8. for (int i = 0; i < dataGridContacts.Rows.Count; i++)
  9. {
  10. dataGridContacts.Rows[i].Cells[0].Selected = true;
  11. dataGridContacts.Rows[i].Cells[0].Value = true;
  12. }
  13. }
  14. if (((CheckBox)sender).Checked == false)
  15. {
  16. for (int i = 0; i < dataGridContacts.Rows.Count; i++)
  17. {
  18. dataGridContacts.Rows[i].Cells[0].Selected = false;
  19. dataGridContacts.Rows[i].Cells[0].Value = false;
  20. }
  21. }
  22. }
  23. clearCheckBox = true;
  24. }
what mistake i did. pls any one can help me
  • Moved byTaylorMichaelLMVPTuesday, September 15, 2009 1:02 AMNot C# specific (From:Visual C# Language)
  •  
rkhema  Monday, September 14, 2009 8:46 AM
Is the header *in* the datagridview? Or row[0]?

If so, should you be looping through your rows starting at row[1]?

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, September 17, 2009 4:46 AM

Hi,

I think it’s better to use DataGridView.CurrentCellDirtyStataChanged event to catch the value changed if you want to get the changed value when users click the cell. Please refer to the remarks part of DataGridViewCheckBoxCell Class.


void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell && dataGridView1.IsCurrentCellDirty)

// or dataGridView1.CurrentCell.ColumnIndex == 0

{

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

int i = 0;

foreach (DataGridViewRow r in dataGridView1.Rows)

{

if (r.Cells["Test"].Value != null)

{

if (((bool)r.Cells["Test"].Value) == true)

Console.WriteLine(i++.ToString());

}

}

if (i == dataGridView1.Rows.Count - 1)

{

chbox.Checked = true;

}

else

{

chbox.Checked = false;

}

}

}

You need also consider when users resize the column wide. The checkbox need to be resized, too. You can create a custom check box head cell.


More information:

Bruce Zhou has written a full demo for this.

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/0572255e-5bf1-4684-947e-526de587c1fa

Column Header CheckBox in DataGridView (Winfoms)

http://www.codeproject.com/KB/grid/DataGridView_winforms.aspx

CheckBox Header Column For DataGridView

http://www.codeproject.com/KB/grid/CheckBoxHeaderCell.aspx

DataGridView.CurrentCellDirtyStateChanged Event

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Monday, September 21, 2009 5:09 AM
Is the header *in* the datagridview? Or row[0]?

If so, should you be looping through your rows starting at row[1]?

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, September 17, 2009 4:46 AM

Hi,

I think it’s better to use DataGridView.CurrentCellDirtyStataChanged event to catch the value changed if you want to get the changed value when users click the cell. Please refer to the remarks part of DataGridViewCheckBoxCell Class.


void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell && dataGridView1.IsCurrentCellDirty)

// or dataGridView1.CurrentCell.ColumnIndex == 0

{

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

int i = 0;

foreach (DataGridViewRow r in dataGridView1.Rows)

{

if (r.Cells["Test"].Value != null)

{

if (((bool)r.Cells["Test"].Value) == true)

Console.WriteLine(i++.ToString());

}

}

if (i == dataGridView1.Rows.Count - 1)

{

chbox.Checked = true;

}

else

{

chbox.Checked = false;

}

}

}

You need also consider when users resize the column wide. The checkbox need to be resized, too. You can create a custom check box head cell.


More information:

Bruce Zhou has written a full demo for this.

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/0572255e-5bf1-4684-947e-526de587c1fa

Column Header CheckBox in DataGridView (Winfoms)

http://www.codeproject.com/KB/grid/DataGridView_winforms.aspx

CheckBox Header Column For DataGridView

http://www.codeproject.com/KB/grid/CheckBoxHeaderCell.aspx

DataGridView.CurrentCellDirtyStateChanged Event

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Monday, September 21, 2009 5:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Maximinze Problem in MDI
• Tab button
• Visibility area of project
• Template count for bitmap arrays
• Disable an item in ListBox?
• Messagebox displayed without reason. Shows exception message, but no exception thrown. The "try catch" block can't catch it.
• Image can't be retreive after ImageList being resized
• problems with showdialog and visible true & false
• Adding rows to DataGridView control problem
• Invisible text in MessageBox (using VB .NET 2003)