c# - 刷新 DataGridView

标签 c# winforms

我有一个绑定(bind)到表的 dataGridView。在与 DataGridView 相同的表单上,我有几个文本框和一个按钮,用于向数据库添加新值并希望它们也显示在 gridView 中。保存在数据库中有效,只是新值没有显示在 gridView 中。我尝试了以下方法:

this.Validate();
this.pBindingSource.EndEdit();
this.pTableAdapter.Update(this.DBDataSet);
pDataGridView.Refresh();

这没有用。根据我的理解,.Refresh 方法只重绘 GridView,所以它应该在更新 tableAdapter 之后工作,所以我想。但事实并非如此。

然后我尝试了:

this.pDataGridView.DataSource = null;
this.pDataGridView.DataSource = this.pBindingSource;
pDataGridView.Refresh();

这也行不通。然后尝试为绑定(bind)源使用 ResetBindings(),但没有用,然后我就没主意了。

如何刷新DataGridView?

最佳答案

你可以这样修改

this.pDataGridView.DataSource = null;
this.pDataGridView.DataBind();
this.pDataGridView.DataSource = this.pBindingSource;
pDataGridView.DataBind();

关于c# - 刷新 DataGridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9402274/

相关文章:

c# - 防止用户通过系统热键退出 Windows 应用程序

c# - 为什么 Application.Current == null 在 WinForms 应用程序中?

c# - 使用 Xamarin Vs Native android 开发 android 应用程序

c# - 返回计数列表并按 id linq to sql 分组

c# - 为什么序列化DataContract时不能使用lambda?

c# - 调试时观察问题中的可枚举

c# - 修剪 DataGridView 单元格中的空白区域

c# - 为什么(发件人为 "something")在 C# Windows Forms 应用程序中具有空值

c# - 我无法注册 ObservableCollection 的事件 CollectionChanged

c# - 使用按钮在两个类之间共享方法