简体   繁体   English

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

[英]Telerik gridview : How to refresh grid view after Database change

I'm using radgridview in C# winform application to show data from database. 我在C#winform应用程序中使用radgridview来显示数据库中的数据。 I'm also altering database through ADO.Net. 我也在通过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. 问题是在更改数据库(例如,通过删除行或添加新行)之后,更改不会出现在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.datasource I got tons of errors. 我还想提到我已通过智能标记将数据库绑定到gridview,并且当我尝试创建新的数据集并将其分配给radgridview1.datasource遇到了很多错误。
Any suggestion on how can force radgridview to reload it's datasource ? 关于如何强制radgridview重新加载其datasource任何建议?

When datasource get changes, to refresh datagrid use following code : 当数据源发生变化时,使用以下代码刷新数据网格:

this.radGridViewName.MasterTemplate.Refresh(null); 

this line solved my problem :-) 这条线解决了我的问题:-)

You can use simple soultion to refresh data in grid: 您可以使用简单的灵魂来刷新网格中的数据:

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

Well, I found the answer myself. 好吧,我自己找到了答案。 Although it only works on dataGridView and doesn't work on dataListView . 尽管它仅适用于dataGridView ,而不适用于dataListView
To delete a record and commit changes to database : 要删除记录并将更改提交到数据库:

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进行相同dataListView ,我将很高兴听到。

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中更新。

This solution is similar to Alexander's: 此解决方案类似于亚历山大的解决方案:

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

ClassOfDataRow is the class used to store one row of data in the grid and radGridView is the name of your RadGridView. ClassOfDataRow是用于在网格中存储一行数据的类,而radGridViewradGridView的名称。

The dataset has a clear function which can be called before new data is passed to the dataset : 数据集具有明确的功能,可以在将新数据传递到数据集之前调用该功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM