我正在使用GridView
,作为第一列,我有一个CheckBox
- 通过选择此CheckBox
,它的行应该保存到数据库:
foreach (DataGridViewRow row in grid_stock.Rows)
{
c = grid_stock.Rows[i].Cells[0];
b = c.EditedFormattedValue == null ? false : bool.Parse(c.EditedFormattedValue.ToString());
if (b != true)
{
c.Value = true;
//here i am inserting the data to sql.,
}
else
{
c.Value=false;
}
}
但在这里我没有进入循环,它没有正确检查 GridView 中的选中项目...
最佳答案
这是您问题的解决方案:
foreach (DataGridViewRow row in grid_stock.Rows)
{
var _value = row.Cells[0].Value;
if (_value != null && !(bool)_value)
{
c.Value = true;
//here i am inserting the data to sql.,
}
else
{
c.Value = false;
}
}
其实你失踪了.Value
对于 row.Cell[0]
,因此在变量 c
中将举行DataGridViewCheckBoxCell
,不包含值。
关于c# - 如何将gridview中选中的行保存到sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835633/