我目前正在使用与wamp服务器连接的datagridview。我的datagridview包含checkboxcolumn(第一列),代码(第二列),课程标题(第三列)和单位(第四列,其中int值在这里)我想知道的是如何获得所有单位的总和被检查过。请帮助我的论文
答案 0 :(得分:0)
foreach (DataGridViewRow row in myDataGrid.Rows)
{
if (row.Cells[myCheckboxCell.Index].Value.ToBool())
{
row.Cells[myCell.index].Value // do somethin to this
}
}
这是我在我的项目中处理我的datagridviews的方法。我希望这有帮助,这不需要你的项目中包含的linq。
但是:
如果您在检查复选框后立即尝试执行value-get thingy并且仅针对该行,则需要将一个委托操作添加到列中,该列已更改。 在那种情况下:
您需要在网格的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;
}
它是我在项目中使用的工具之一。