更新后如何刷新 c# dataGridView? [英] How can I refresh c# dataGridView after update ?

查看:61
本文介绍了更新后如何刷新 c# dataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击任何一行时,我有一个 dataGridView,一个表单被打开以更新行数据,但在结束更新后,更新表单关闭但 dataGridView 数据没有更新

I have a dataGridView when I click on any row a form is opened to update the row data, but after ending updates the updating form is closed but the dataGridView data is not updated

我该怎么做?

推荐答案

BindingSource 是不使用第 3 方 ORM 的唯一方法,乍一看似乎冗长,但一次更新的好处BindingSource 上的方法非常有用.

BindingSource is the only way without going for a 3rd party ORM, it may seem long winded at first but the benefits of one update method on the BindingSource are so helpful.

如果您的来源是例如用户字符串列表

If your source is say for example a list of user strings

List<string> users = GetUsers();
BindingSource source = new BindingSource();
source.DataSource = users;
dataGridView1.DataSource = source;

然后,当您完成编辑时,只需更新您的数据对象,无论是 DataTable 还是像此处这样的用户字符串列表和 BindingSource 上的 ResetBindings>;

then when your done editing just update your data object whether that be a DataTable or List of user strings like here and ResetBindings on the BindingSource;

users = GetUsers(); //Update your data object
source.ResetBindings(false);

这篇关于更新后如何刷新 c# dataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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