C# 问题:如何将 DataGridView 中所做的更改保存回使用的 DataTable? [英] C# Issue: How do I save changes made in a DataGridView back to the DataTable used?

查看:36
本文介绍了C# 问题:如何将 DataGridView 中所做的更改保存回使用的 DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

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

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

提前致谢!

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

Let me know if you need me to elaborate more.

推荐答案

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

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

这是一个例子:

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);
        }

    }
}

这篇关于C# 问题:如何将 DataGridView 中所做的更改保存回使用的 DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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