C# Issue: How do I save changes made in a DataGridView back to the DataTable used?(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
,那么您已经更新了 数据表
/数据集
.如果您的意思是对数据库进行更改,那么这就是适配器发挥作用的地方.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 问题:如何将在 DataGridView 中所做的更改保存回使用的 DataTable?


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01