Datagridview更新

卡尔提克·雷迪

如何更新DataGridView使其也将影响数据库的更改?我正在尝试的代码是:

foreach (DataGridViewRow myDgrow in dataGridView2.Rows) {
    myCmd = "Update Details set ProjectName='" 
          + myDgrow.Cells["ProjectName"].Value 
          + "', Description = '" 
          + myDgrow.Cells["Description"].Value 
          + "', DateStarted='" 
          + myDgrow.Cells["DateStarted"].Value 
          + "',TeamSize='" 
          + myDgrow.Cells["TeamSize"].Value 
          + "',Manager='" 
          + myDgrow.Cells["Manager"].Value 
          + "'";

    myCmd = "Update Details set Description = '" 
          + myDgrow.Cells["Description"].Value 
          + "', DateStarted='" 
          + myDgrow.Cells["DateStarted"].Value 
          + "',TeamSize='" 
          + myDgrow.Cells["TeamSize"].Value 
          + "',Manager='" 
          + myDgrow.Cells["Manager"].Value 
          + "' where ProjectName='" 
          + myDgrow.Cells["ProjectName"].Value 
          + "'";

    cmd.Parameters.AddWithValue("@projectName1", myDgrow.Cells["ProjectName"].Value);
    cmd.Parameters.AddWithValue("@Description1", myDgrow.Cells["Description"].Value);
    cmd.Parameters.AddWithValue("@DateStarted1", myDgrow.Cells["DateStarted"].Value);
    cmd.Parameters.AddWithValue("@TeamSize1", myDgrow.Cells["TeamSize"].Value);
    cmd.Parameters.AddWithValue("@Manager1", myDgrow.Cells["Manager"].Value);
    cmd.CommandText = myCmd;

    dataGridView2.Update();

    //cmd.Parameters.Clear();
    cmd.ExecuteNonQuery();
    myCmd = string.Empty;
}
迈克·佩伦努德(Mike Perrenoud)

好的,这就是您要执行的操作:

using (SqlConnection c = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(sql, c))
{
    cmd.Parameters.AddWithValue("@field1", myDgrow.Cells["field1"].Value);
    ...

    cmd.ExecuteNonQuery();
}

其中sql可能看起来是这样的:

UPDATE table SET field1 = @field1, field2 = @field2 WHERE fieldId = @fieldId

并且您将在foreach循环中的每次迭代中执行此操作

老实说,我不知道您在代码中正在做什么,因为您将myCmd背对背设置为两个不同的东西,然后又没有使用它。所以我不知道该cmd对象具有什么SQL 因此,只需修改您的代码以使用我提出的结构,它便会按预期工作。

注意:我不知道是否允许用户添加到数据网格,但是如果允许,您将构建一个不同的表,sql因为它需要是一条INSERT语句。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

快速更新的DataGridView

来自分类Dev

用于更新DataGridView的线程

来自分类Dev

从WindowForm更新datagridview

来自分类Dev

自动更新datagridview

来自分类Dev

读取XML并更新DataGridView

来自分类Dev

增量更新DataGridView单元

来自分类Dev

更新DataGridView列值

来自分类Dev

Sql更新使datagridview消失

来自分类Dev

DataGridView将无法正确更新

来自分类Dev

DataGridView从更新中跳过列

来自分类Dev

从DataGridView更新数据时出错

来自分类Dev

EF 4.0 Datagridview无法更新

来自分类Dev

更新每个选定的datagridview行

来自分类Dev

在datagridview中更新ComboBox列

来自分类Dev

更新DataGridView还是更新绑定的DataSource?

来自分类Dev

Datagridview列索引在更新后返回0

来自分类Dev

在已打开的表单上未更新Datagridview

来自分类Dev

C#DataGridView不使用DataTable更新

来自分类Dev

子窗体关闭后更新DataGridView

来自分类Dev

通过文本框更新datagridview

来自分类Dev

无法通过DataBoundItem更新DataGridView单元吗?

来自分类Dev

在CellValueChanged期间winform datagridview更新值

来自分类Dev

在新表格上更新datagridview中的数据

来自分类Dev

DataGridView1被替换而不是更新

来自分类Dev

如何仅更新dataGridview中的数据

来自分类Dev

Datagridview在已打开的表单上未更新

来自分类Dev

无法转换参数值-更新对DataGridView的更改

来自分类Dev

dataGridView不会反映更新的数据集

来自分类Dev

通过datagridview vb更新多表数据集