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?
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
