2016-05-25 6 views
0

cを私は3 DatagridviewCheckboxCellを持っていると私はそれらを確認するために、このコードを使用:はDatagridviewCheckboxCellをチェック - #

foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      DataGridViewCheckBoxCell chk1 = (DataGridViewCheckBoxCell)row.Cells["Column6"]; 
      DataGridViewCheckBoxCell chk2 = (DataGridViewCheckBoxCell)row.Cells["Column7"]; 
      DataGridViewCheckBoxCell chk3 = (DataGridViewCheckBoxCell)row.Cells["Column8"]; 

      string commande2 = "SELECT id_pres from Fonct_Prest where id_fonc = @id"; 
      OleDbCommand cmd2 = new OleDbCommand(commande2, con); 
      cmd2.Parameters.AddWithValue("@id", row.Cells["Code"].Value); 
      dt2.Load(cmd2.ExecuteReader()); 

      for (int i = 0; i < dt2.Rows.Count; i++) 
      { 
       if (dt2.Rows[i][0].ToString() == "1") 
       { 
        chk1.Value = true; 
       } 
       if (dt2.Rows[i][0].ToString() == "2") 
       { 
        chk2.Value = true; 
       } 
       if (dt2.Rows[i][0].ToString() == "6") 
       { 
        chk3.Value = true; 
       } 
      } 
      cmd2.Parameters.Clear(); 
      dt2.Rows.Clear(); 
     } 

このコードは正常に動作しますが、DataGridViewのの最初の行のチェックボックスがチェックされていません。私のコードのどこにエラーがありますか?

答えて

0

カラム名がわかっている場合は、このコードを試してみてください。

private void button2_Click(object sender, EventArgs e) 
    { 
     for(int i = 0; i < dataGridView1.RowCount; i++) 
     { 
      //Set defined value 
      //dataGridView1.Rows[i].Cells["Column2"].Value = true; 

      //toggle value from checked to unchecked. 
      dataGridView1.Rows[i].Cells["Column2"].Value = !Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column2"].Value); 
     } 
    } 
+0

これを試しましたが、同じ動作ですが、DataGridviewの最初の行のチェックボックスはチェックされませんでした – user4340666

関連する問題