winform經常用datagridview來處理相關的數據顯示,如果datagridview有復選框,我們應該如何處理相關選中響應。選擇datagridview的cellcontentclick事件,代碼如下:
bool isSelectedGridViewRow = false;
?private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
?{?
? ? ? ?? //只允許選一個
? ? ? ? if (e.ColumnIndex == 0 && e.RowIndex >= 0)
? ? ? ??{
? ? ? ? ? ? ? ? //如果選擇所屬的列是第1列那么 ColumnIndex == 0
? ? ? ? ? ? ? ? foreach (DataGridViewRow row in dataGridView1.Rows)?
? ? ? ? ? ? ?? ?row.Cells[e.ColumnIndex].Value = false;
? ? ? ? ? ? ? ? dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
? ? ? ? ?}
? ? ? ? ?isSelectedGridViewRow = false;
? ? ? ? ? ?//遍歷
? ? ? ? ? ?foreach (DataGridViewRow dr in this.dataGridView1.Rows)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //所有的都判斷
? ? ? ? ? ? ? ? ? ? if ((bool)dr.Cells[0].EditedFormattedValue)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? isSelectedGridViewRow = true;
? ? ? ? ? ? ? ? ? ? ? ?//你的處理
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }