刷新DataGridView

3

我有一个绑定到表格的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();

这也不起作用。然后尝试对绑定源进行ResetBindings(),但它也无法正常工作,然后我就没有了更多的想法。

如何刷新DataGridView?


它没有起作用。不过还是谢谢你的输入。 - orzac sergiu
第一个问题:Silverlight还是WPF? 第二个问题:您用什么类型进行数据收集 - pBindingSource? - Meonester
  1. 标题上说的是C#伙计。
  2. private System.Windows.Forms.BindingSource pBindingSource。
- orzac sergiu
这是一个Windows窗体应用程序吗?还是其他类型的应用程序? - Rémi
@Meonester Windows Forms支持数据绑定。我认为他甚至不知道Silverlight和WPF是什么。 - 15ee8f99-57ff-4f92-890c-b56153
显示剩余2条评论
2个回答

1

您可以像这样进行修改

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

pDataGridView 没有 .DataBind() 方法。我是用的 Visual Studio 2010 - orzac sergiu
3
请注意:VS的版本与C#的特性完全无关。我认为你应该说明你使用的.NET框架版本。 - gunr2171

0

你也可以使用这个:

this.[tablename]tableAdapter.Fill(this.[Database]DataSet.[tablename]);

在每个按钮命令中,至少对我有效。

例如,我的代码看起来像这样:

this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接