datagridview选中获取行号_如何获取datagridview中checkbox选中的多行数据

本文介绍了在C# WinForm开发中,如何有效地判断和获取DataGridView中Checkbox列选中的行号及其数据。通过遍历Rows并检查Cells的EditedFormattedValue,实现对选中行的判断和操作。同时提供了CellMouseClick事件处理方法,用于切换Checkbox状态。
摘要由CSDN通过智能技术生成

开发(winform)时碰到这个问题 C# 怎样判断 datagridview 中的checkbox列是否被选中,错误原因都C#对类型判断相当严格,为了避免大家以后多走弯路,把我的思路和大家说一下,希望对大家有帮助。

for (int i = 0; i < dataGridView1.Rows.Count; i++)

{

if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue==true)

{

//TODO

}

}

===================================================================================

private void PrintInFo() {

try

{

int count = 0;

//用于保存选中的checkbox数量

//DG_List为datagridview控件

for (int i = 0; i < DG_List.RowCount; i++)

{

if (DG_List.Rows[i].Cells  [0].EditedFormattedValue.ToString() == "True")

//这里判断复选框是否选中

{

count++;

}

}

if (count == 0)

{

MessageBox.Show("请至少选择一条数据!", "提示");

return;

}

else

{

if (MessageBox.Show(this, "您要更新数据么?", "提示", MessageBoxButtons.YesNo,    MessageBoxIcon.Information).ToString() == "Yes")

{

for (int i = 0; i < count; i++)

{

ps.Pexcute(" update cf_prj_certi set FIsPrint='"+number+"' where fid='" +  DG_List.Rows[i].Cells["fnn"].Value.ToString() + "'"); //执行SQL

}

} else

{

return;

}

}

} catch (Exception ex)

{

MessageBox.Show(ex.ToString());

} this.ShowInfo(); //重新绑定datagridview

}

=========================================

点击dataGridView1事件中的CellMouseClick 事件

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

if(e.ColumnIndex==6)

{

dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值