Solution1:
//In Fill DataGridViewEvent :spa
DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxColumn(); ChCol.Name = "CheckBoxRow"; ChCol.HeaderText = "CheckboxSelection"; ChCol.Width = 50; ChCol.TrueValue = "1"; ChCol.FalseValue = "0"; datagridview_tabpage1.Columns.Insert(0, ChCol);
// In Button Event put these codes:code
datagridview 中的checkbox列是否被選中orm
private void button3_Click(object sender, EventArgs e) { string selectRows=""; for (int i = 0; i < dataGridView_tabPage1.Rows.Count - 1; i++) //循環datagridview每行 { if ((bool)dataGridView_tabPage1.Rows[i].Cells[0].EditedFormattedValue == true) { selectRows = selectRows + "[" + i.ToString() + "]"; } } MessageBox.Show("Selected Rows:" + selectRows,"CheckBoxRows"); }
add check box in Datagridview
// Initialize and add a check box column. DataGridViewColumn column = new DataGridViewTextBoxColumn(); column = new DataGridViewCheckBoxColumn(); column.DataPropertyName = "selection"; column.Name = "selection"; dataGridView_tabPage1.Columns.Add(column);
全選
//循環dataGridView for (int i = 0; i < dataGridView_tabPage1.Rows.Count; i++) { //設置設置每一行的選擇框爲選中,第一列爲checkbox dataGridView_tabPage1.Rows[i].Cells[0].Value = true; }
反選blog
//循環dataGridView for (int i = 0; i <dataGridView_tabPage1.Rows.Count; i++) { //判斷當前行是否被選中 if ((bool)dataGridView_tabPage1.Rows[i].Cells[0].EditedFormattedValue == true) //設置每一行的選擇框爲未選中 dataGridView_tabPage1.Rows[i].Cells[0].Value = false; else //設置每一行的選擇框爲選中 dataGridView_tabPage1.Rows[i].Cells[0].Value = true; }