詳解C# CheckBox選中的判斷方法
C# CheckBox選中的判斷方法是在做有關C# winform的時候使用Windows DataGridView來實現的,具體的是在DataWindow中增加新行.實現方法是什么呢?那么這里向你詳細介紹。
C# CheckBox選中的判斷方法實現方法:
右擊菜單后彈出一窗體,新窗體上有一個DataGridView ,***列是個DataGridViewCheckBoxColumn列.要求是選中checkbox的行添加到父窗體數據源中.現就判斷哪些有選中的
C# CheckBox選中的判斷方法實例演示:
- foreach (DataGridViewRow dr in this.dataGridView1.Rows)
- {
- try
- {
- //DataGridViewCheckBoxCell cbx =
- (DataGridViewCheckBoxCell)dr.Cells[0];
- //if ((bool)cbx.FormattedValue)
- if(dr.Cells[0].Selected)
- {
- arrShiftCode.Add(dr.Cells[1].Value);
- arrShiftGroup.Add(dr.Cells[2].Value);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
以上是一開始這樣寫的,發現選中了多個,始終只有***一個是True,其他的都是False.***經查資料有如下寫法即可
- foreach (DataGridViewRow dr in this.dataGridView1.Rows)
- {
- try
- {
- DataGridViewCheckBoxCell cbx =
- (DataGridViewCheckBoxCell)dr.Cells[0];
- if ((bool)cbx.FormattedValue)
- {
- arrShiftCode.Add(dr.Cells[1].Value);
- arrShiftGroup.Add(dr.Cells[2].Value);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
C# CheckBox選中的判斷方法的相關內容就向你介紹到這里,希望對你了解C# CheckBox選中的判斷方法有所幫助。
【編輯推薦】