詳解C# CheckBox選中的判斷方法
C# CheckBox選中的判斷方法是在做有關(guān)C# winform的時(shí)候使用Windows DataGridView來實(shí)現(xiàn)的,具體的是在DataWindow中增加新行.實(shí)現(xiàn)方法是什么呢?那么這里向你詳細(xì)介紹。
C# CheckBox選中的判斷方法實(shí)現(xiàn)方法:
右擊菜單后彈出一窗體,新窗體上有一個(gè)DataGridView ,***列是個(gè)DataGridViewCheckBoxColumn列.要求是選中checkbox的行添加到父窗體數(shù)據(jù)源中.現(xiàn)就判斷哪些有選中的
C# CheckBox選中的判斷方法實(shí)例演示:
- 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);
- }
- }
以上是一開始這樣寫的,發(fā)現(xiàn)選中了多個(gè),始終只有***一個(gè)是True,其他的都是False.***經(jīng)查資料有如下寫法即可
- 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選中的判斷方法的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解C# CheckBox選中的判斷方法有所幫助。
【編輯推薦】