2011-09-26 65 views
0

我要选中一个复选框,这是GridView的程序里面的一个按钮的点击选中复选框内GridView的

+0

尽量按照这个线程 http://stackoverflow.com/questions/1237829/datagridview-checkbox-column-value-and-functionality 此致敬礼。 –

+0

你指的是点击按钮上的所有复选框? –

回答

0

这为我工作:

for (int i = 0; i < dataGridView1.RowCount - 1; i++) 
      { 
       dataGridView1.Rows[i].DataGridView[0, i].Value = false; 
      } 
0

尝试这种方式(通过订阅两个事件):

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) 
    { 
     if (dataGridView1.IsCurrentCellDirty) 
      dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 
    } 

    private void dataGridView1_CellValueChanged(object obj, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex == 1) //compare to checkBox column index 
     { 
      DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex]; 
      if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true) 
      {     
       //checkBox is checked - do the code in here! 
      } 
      else 
      { 
       //if checkBox is NOT checked (unchecked) 
      } 
     } 
    } 
+0

对不起,我已编辑我的帖子,请重新检查 –

1

如果你知道电池的位置,然后你可以将其设置为

datagridview1[0,2].Value = true;// this should be a checkboxcolumn 

OR

datagridview1["chkboxcolumnName",2].Value = true; 

这将导致检查该特定单元格的复选框。

希望这是你的意思别的,请编辑问题的更多细节。