C# Telerik gridview:如何在数据库更改后刷新网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16606911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:26:57  来源:igfitidea点击:

Telerik gridview : How to refresh grid view after Database change

c#winformstelerikdatasettelerik-grid

提问by Rsh

I'm using radgridviewin C# winform application to show data from database. I'm also altering database through ADO.Net. The problem is after I change the database, for example by deleting a row or adding a new row, changes do not appear in gridview.
I also want to mention that I have bound database to gridview through smart tags and when I tried to create a new dataset and assign it to radgridview1.datasourceI got tons of errors.
Any suggestion on how can force radgridviewto reload it's datasource?

radgridview在 C# winform 应用程序中使用来显示数据库中的数据。我也在通过 ADO.Net 更改数据库。问题是在我更改数据库后,例如通过删除一行或添加一个新行,更改不会出现在 gridview 中。
我还想提一下,我已经通过智能标签将数据库绑定到 gridview,当我尝试创建一个新数据集并将其分配给radgridview1.datasource我时,我遇到了大量错误。
关于如何强制radgridview重新加载它的任何建议datasource

采纳答案by Rsh

Well, I found the answer myself. Although it only works on dataGridViewand doesn't work on dataListView.
To delete a record and commit changes to database :

嗯,我自己找到了答案。虽然它只dataGridViewdataListView.
要删除记录并将更改提交到数据库:

radGridView1.CurrentRow.Delete();
this.yourTableAdapter.Update(yourDataSet);

On the other hand, if you have added new records and you want to reform the list :

另一方面,如果您添加了新记录并且想要重新排列列表:

this.yourTableAdapter.Fill(yourDataSet.yourTabel);

If you know how to do the same with dataListView, I'll be glad to hear.

如果你知道如何用 做同样的事情dataListView,我会很高兴听到的。

回答by alexmac

You can use simple soultion to refresh data in grid:

您可以使用简单的 solltion 来刷新网格中的数据:

MyGrid.DataSource = null;
MyGrid.DataSource = updatedData;

回答by checho

Here is a tutorial, explaining in a step by step manner how to bind the grid. Once it is bound, changes introduced to the underlying source will be automatically reflected and changed in RadGridView will be updated in the DataTable after you Update the TableAdapter.

这是一个教程,逐步解释如何绑定网格。绑定后,引入到底层源的更改将自动反映,并且在更新 TableAdapter 后,RadGridView 中的更改将在 DataTable 中更新。

回答by Priyanka

When datasource get changes, to refresh datagrid use following code :

当数据源发生变化时,刷新数据网格使用以下代码:

this.radGridViewName.MasterTemplate.Refresh(null); 

this line solved my problem :-)

这条线解决了我的问题:-)

回答by Boabashint

This solution is similar to Alexander's:

这个解决方案类似于亚历山大的:

List<ClassOfDataRow> t = radGridView.ItemsSource as List<ClassOfDataRow>;
radGridView.ItemsSource = null;
radGridView.ItemsSource = t;

ClassOfDataRowis the class used to store one row of data in the grid and radGridViewis the name of your RadGridView.

ClassOfDataRow是用于在网格中存储一行数据的类,并且radGridView是您的 RadGridView 的名称。

回答by wsduho

The dataset has a clear function which can be called before new data is passed to the dataset :

数据集有一个 clear 函数,可以在将新数据传递给数据集之前调用该函数:

Resultset.Clear();
DataAdapter.fill(Resultset);
Radgridview.datasource=Resultset;