如何从datagridview(带checkboxvolumen)中获取int值;s复选框被选中

本文关键字:int 复选框 获取 datagridview checkboxvolumen | 更新日期: 2023-09-27 18:29:01

我目前正在使用与wamp服务器连接的datagridview。我的datagridview包含checkbox列(第1列)、代码(第2列)、课程标题(第3列)和单元(第4列,这里有int值)我想知道的是如何获得所有已检查单元的总和。请帮忙,这是我的论文

如何从datagridview(带checkboxvolumen)中获取int值;s复选框被选中

        foreach (DataGridViewRow row in myDataGrid.Rows)
{
         if (row.Cells[myCheckboxCell.Index].Value.ToBool())
         {
             row.Cells[myCell.index].Value // do somethin to this
         }
}

这就是我在项目中处理数据网格视图的方式。我希望这有帮助,这不需要在你的项目中包含linq。

但是:

如果您试图在选中复选框之后立即执行值getthingy,并且仅针对该行,则需要向列中添加一个委派操作,该操作将被选中并更改。在这种情况下:

您需要在网格的EditingControlShowing事件中使用此项

 var checkbox = (CheckBox)e.Control;
    checkbox.CheckedChanged += (sender2, e2) =>
    {
         if(checkbox.Checked)
         {
            myDatagrid.CurrentRow.Cells[myIntValue.index].Value //do something to your value
         }
    };

这意味着您将复选框转换为普通复选框,并使委托选中的更改事件为它,这样,每次选中它时,您的值都会被选中,您所需的事件也会起作用。

编辑:这就是ToBool()的作用:

public static bool ToBool(this bool? ex)
    {
        if (ex == null || ex == false)
            return false;
        return true;
    }

它是我在项目中使用的工具之一。