C# 问题:如何将 DataGridView 中所做的更改保存回使用的 DataTable?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:33:41  来源:igfitidea点击:

C# Issue: How do I save changes made in a DataGridView back to the DataTable used?

c#datagridbindingdatatabledataset

提问by OneShot

I get a DataTable from a DataSet and then bind that DataTable to a DataGridView. Once the user edits the information on the DataGridView how do I take those changes and put them back into a DataTable that was used that I can then put back into my DataSet?

我从 DataSet 中获取 DataTable,然后将该 DataTable 绑定到 DataGridView。一旦用户编辑了 DataGridView 上的信息,我如何进行这些更改并将它们放回已使用的 DataTable 中,然后我可以放回我的 DataSet?

I want to make a Save Button on my DataGrid that when pressed actually saves the changes.

我想在我的 DataGrid 上创建一个保存按钮,当按下时实际保存更改。

I don't if I can get anymore specific than that, because it is a fairly simple question.

如果我能得到比这更具体的,我不知道,因为这是一个相当简单的问题。

Thanks in advance!

提前致谢!

Let me know if you need me to elaborate more.

如果您需要我详细说明,请告诉我。

采纳答案by Marc Gravell

If you are using data-binding to a DataGridView, then you are alreadyupdating the DataTable/ DataSet. If you mean changes down to the database, then that is where adapters come into play.

如果您使用数据绑定到 a DataGridView,那么您已经在更新DataTable/ DataSet。如果您的意思是对数据库进行更改,那么这就是适配器发挥作用的地方。

Here's an example:

下面是一个例子:

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();

        DataSet set = new DataSet();
        DataTable table = set.Tables.Add("MyTable");
        table.Columns.Add("Foo", typeof(int));
        table.Columns.Add("Bar", typeof(string));

        Button btn;
        using (Form form = new Form
        {
            Text = "DataGridView binding sample",
            Controls =
            {
                new DataGridView {
                    Dock = DockStyle.Fill,
                    DataMember = "MyTable",
                    DataSource = set
                },
                (btn = new Button {
                    Dock = DockStyle.Bottom,
                    Text = "Total"
                })
            }
        })
        {
            btn.Click += delegate
            {
                form.Text = table.AsEnumerable().Sum(
                    row => row.Field<int>("Foo")).ToString();
            };
            Application.Run(form);
        }

    }
}

回答by kpollock

as mentioned DataAdapters are one of the easy ways.

如前所述,DataAdapters 是一种简单的方法。

See: http://www.codeproject.com/KB/database/relationaladonet.aspx

请参阅:http: //www.codeproject.com/KB/database/relationaladonet.aspx

for a pretty simple example that I think covers what youu need.

对于一个非常简单的例子,我认为它涵盖了你所需要的。