如何使用DataGridView的绑定源C#来更新SQL Server数据库 [英] How to update SQL Server database using Datagridview binding source C#

查看:235
本文介绍了如何使用DataGridView的绑定源C#来更新SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#WinForms应用程序,允许用户编辑和使用的datagridview更新数据库。



现在的问题是,我不能让上班。我设法实现的唯一事情是什么更新在DataGridView是显示,但是当我进入数据库表中,在数据没有变化。
我查了很多网站讨论有关尚未工作对我来说这个问题,但没有的。



事情到目前为止,我曾尝试:




  • 数据库绑定到DataGridView

  • 修改复制到输出目录属性复制如果新

  • 使用 dataAdapter.Update((数据表)bindingSource1.DataSource) 有或没有任何更新查询执行。

  • 执行更新查询,而不 dataAdapter.update(...)



下面是我的代码:

 公共Form1中()
{
的InitializeComponent();
的GetData(SELECT * FROM表1);
}

无效的GetData(字符串的SelectCommand)
{
SqlConnection的康恩=新的SqlConnection(Properties.Settings.Default.NewDBTESTConnectionString);

= DataAdapter的新的SqlDataAdapter(SelectCommand中,康恩);
CommandBuilder的=新SqlCommandBuilder(DataAdapter的);

表=新的DataTable();
DataAdapter.Fill方法(表);

bindingSource1.DataSource =表;
dataGridView1.DataSource = bindingSource1;
}

私人无效dataGridView1_CellEndEdit(对象发件人,DataGridViewCellEventArgs E)
{
dataAdapter.Update((数据表)bindingSource1.DataSource);
}


解决方案

没有调用 bindingSource1.EndEdit 您的基础DataTable没有看到任何改变,因此更新命令没有任何更新。

 私人无效dataGridView1_CellEndEdit(对象发件人,DataGridViewCellEventArgs E)
{
bindingSource1.EndEdit();
DataTable的DT =(数据表)bindingSource1.DataSource;

//只是为了测试....使用或不使用EndEdit中试试这个....
的DataTable changedTable = dt.GetChanges();
Console.WriteLine(changedTable.Rows.Count);

INT rowsUpdated = da.Update(DT);
Console.WriteLine(rowsUpdated);
}


I'm writing a Winforms app in C# that enables the user to edit and update database using the datagridview.

The problem is, I can't make it to work. The only thing that I managed to achieve is updating what the datagridview is showing, but when I go into the database table, there is no change in the data. I have search a lot of sites discussing about that problem but nothing yet worked for me.

Things I have tried so far:

  • binding the database to the datagridview
  • changing the Copy to Output Directory property to Copy if newer
  • using the dataAdapter.Update((DataTable)bindingSource1.DataSource) with or without any update query execute.
  • execute update query without dataAdapter.update(...).

Here is my code:

public Form1()
{
    InitializeComponent();
    GetData("SELECT * FROM Table1");
}

void GetData(string selectCommand)
{
    SqlConnection conn = new SqlConnection(Properties.Settings.Default.NewDBTESTConnectionString);

    dataAdapter = new SqlDataAdapter(selectCommand, conn);
    commandBuilder = new SqlCommandBuilder(dataAdapter);

    table = new DataTable();
    dataAdapter.Fill(table);

    bindingSource1.DataSource = table;
    dataGridView1.DataSource = bindingSource1;
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
        dataAdapter.Update((DataTable)bindingSource1.DataSource);    
}

解决方案

Without calling bindingSource1.EndEdit your underlying DataTable doesn't see any change and thus the Update command has nothing to update.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    bindingSource1.EndEdit();
    DataTable dt = (DataTable)bindingSource1.DataSource;

    // Just for test.... Try this with or without the EndEdit....
    DataTable changedTable = dt.GetChanges();
    Console.WriteLine(changedTable.Rows.Count);

    int rowsUpdated = da.Update(dt);    
    Console.WriteLine(rowsUpdated);
}

这篇关于如何使用DataGridView的绑定源C#来更新SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆