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?
基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
