locked
Set DataGridViewCheckBoxCell Value to True

    Question

  • HI,

    Im working with a DGV which is bound to a sql datasource. It has an unbound column which is DataGridViewCheckBoxColumn, I want to set all the checkbox to checked when the DGV is initialized.

    Here is my code

     foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    DataGridViewCheckBoxCell temp_Check_Cell = (DataGridViewCheckBoxCell)row.Cells["C_ChkMtr"];
    temp_Check_Cell.Value = true;

    }

    However, it doesn't work.

    I've gone through the entire debug process, the value of the cell remains true until   the showdialog() is called. I also found out that if I add the code above to a

    button, then press the button, all the check boxes will be checked, which means the code works after showdialog() not before.

     

    Does anyone know what's wrong?

     

    Thank you

     

    Friday, November 12, 2010 6:41 PM

Answers

  • hi my friend.

    you can set checkbox value to true whenever your datagrid  DataBindingComplete event raised ,

     

      private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
            DataGridViewCheckBoxCell temp_Check_Cell = (DataGridViewCheckBoxCell)row.Cells["C_ChkMtr"];
            temp_Check_Cell.Value = true;
          }
    
        }
    

     

     


    VS.Net adventurer ----------------- If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by wyzbs Monday, November 15, 2010 7:03 PM
    Sunday, November 14, 2010 7:45 AM

All replies

  • WHere does the above foreach loop reside? Ctor? Form_load? Could you post that method/event

     

    Friday, November 12, 2010 11:00 PM
  • Too bad you didn't post code that demonstrates what you are talking about.  " ShowDialog() "  It is difficult to follow what you are talking about.
    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Saturday, November 13, 2010 10:56 PM
    Moderator
  • hi my friend.

    you can set checkbox value to true whenever your datagrid  DataBindingComplete event raised ,

     

      private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
            DataGridViewCheckBoxCell temp_Check_Cell = (DataGridViewCheckBoxCell)row.Cells["C_ChkMtr"];
            temp_Check_Cell.Value = true;
          }
    
        }
    

     

     


    VS.Net adventurer ----------------- If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by wyzbs Monday, November 15, 2010 7:03 PM
    Sunday, November 14, 2010 7:45 AM
  • Thank you Vali.

     

    Your solutions works fine.

    Monday, November 15, 2010 7:03 PM
  • thanks my dear friend.

    i am very glad that i could to help my friend.


    VS.Net adventurer ----------------- If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, November 15, 2010 7:33 PM