c# - 如何将gridview中选中的行保存到sql数据库

标签 c# winforms gridview

我正在使用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/

相关文章:

c# - 在 Realm 中使用特定于项目的 ViewModel 的最佳方式是什么?

c# - Web API发布返回415-不支持的媒体类型

java - 如何访问父 Activity 中的 fragment 布局?

c# - 无法在表单中找到 {Microsoft.VisualBasic.PowerPacks.RectangleShape} 控件

android - gridview 在 notifiedDatasetChangedCalled 时自动更改位置?

c# - GridView分页问题

c# - 创建项目 ASP.NET Core(.NET Core)和 ASP.NET Core(.NET Framework)有什么区别

c# - 如何捕获此 WPF 位图加载异常?

c# - 即使新旧索引相同,是否也可以触发 ComboBox SelectedIndex Changed 事件?

c# - WinForms - 在 TableLayoutPanel 中重叠两个控件