2011-09-26 78 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; 

這將導致檢查該特定單元格的複選框。

希望這是你的意思別的,請編輯問題的更多細節。