Solution1:
//In Fill DataGridViewEvent :

DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxColumn();
ChCol.Name = "CheckBoxRow";
ChCol.HeaderText = "CheckboxSelection";
ChCol.Width = ;
ChCol.TrueValue = "";
ChCol.FalseValue = "";
datagridview_tabpage1.Columns.Insert(, ChCol);

// In Button Event put these codes:

datagridview 中的checkbox列是否被选中

private void button3_Click(object sender, EventArgs e)
{
string selectRows="";
for (int i = ; i < dataGridView_tabPage1.Rows.Count - ; i++) //循环datagridview每行
{
if ((bool)dataGridView_tabPage1.Rows[i].Cells[].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 = ; i < dataGridView_tabPage1.Rows.Count; i++)
{
//设置设置每一行的选择框为选中,第一列为checkbox
dataGridView_tabPage1.Rows[i].Cells[].Value = true;
}

反选

//循环dataGridView
for (int i = ; i <dataGridView_tabPage1.Rows.Count; i++)
{
//判断当前行是否被选中
if ((bool)dataGridView_tabPage1.Rows[i].Cells[].EditedFormattedValue == true)
//设置每一行的选择框为未选中
dataGridView_tabPage1.Rows[i].Cells[].Value = false;
else
//设置每一行的选择框为选中
dataGridView_tabPage1.Rows[i].Cells[].Value = true;
}
 
05-08 08:09