Datagridview 添加checkbox列,并判断Datagridview 中的checkbox列是否被选中

这篇博客详细介绍了如何在 C# 的 DataGridView 控件中添加、全选和反选 Checkbox 列。通过插入 DataGridViewCheckBoxColumn,设置列属性,并在按钮事件中实现选择行的判断和显示。此外,还提供了初始化并添加 Checkbox 列的方法。
摘要由CSDN通过智能技术生成

Solution1:
//In Fill DataGridViewEvent :

复制代码

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:

datagridview 中的checkbox列是否被选中

复制代码

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;
   }
 

反选

复制代码

//循环dataGridView
for (int i = 0; i 

复制代码

 

 

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值