简体   繁体   English

Telerik RadGrid中的数据未使用LINQ-to-SQL更新(奇怪的行为)

[英]Data in Telerik RadGrid not updated using LINQ-to-SQL (strange behavior)

I've found a strange behavior with Telerik RadGrid Datasource using Microsoft LINQ To Sql, which I'd like to know what reason causes it. 我发现使用Microsoft LINQ To Sql的Telerik RadGrid数据源有一个奇怪的行为,我想知道是什么原因导致它。

Suppose I've a basic grid with OnNeedDataSource event: 假设我有一个OnNeedDataSource事件的基本网格:

<telerik:RadGrid ID="grid" runat="server" OnNeedDataSource="grid_NeedDataSource">
...
</telerik:RadGrid>

Event handler in code-behind: 代码隐藏中的事件处理程序:

protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    grid.DataSource = Dal.Db.VIEW_DETAILs;
}

On the Microsoft Sql Server, VIEW_DETAIL is defined as a view (which select query joins multiple ordinary tables). 在Microsoft Sql Server上,VIEW_DETAIL被定义为视图(选择查询连接多个普通表)。 One of the source tables (let's say it's name is DETAIL) for the view query uses also a computed column that's present also in the view. 视图查询的其中一个源表(假设它的名称为DETAIL)也使用了一个在视图中也存在的计算列。 Querying the view directly from Sql always returns consistent and updated results. 直接从Sql查询视图始终返回一致和更新的结果。

When in my program the source table DETAIL is updated, I call normally 在我的程序中,源表DETAIL更新,我正常调用

grid.DataBind() 

to update its content. 更新其内容。 Surprisingly nothing is updated (for instance the computed column mentioned above remains the old value). 令人惊讶的是,没有更新任何内容(例如,上面提到的计算列仍然是旧值)。 After dealing with some workarounds, I've found that changing the source above on the grid_NeedDataSource handler to 在处理了一些变通方法之后,我发现将grid_NeedDataSource处理程序上面的源代码更改为

protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    grid.DataSource = Dal.Db.VIEW_DETAILs.Where(x=> 1 == 1);
}

(eg adding a Where clause that is always true) the data now is correctly updated. (例如,添加始终为true的Where子句)现在可以正确更新数据。

Any insights on this behavior? 有关此行为的任何见解?

Try calling Rebind() instead of DataBind() . 尝试调用Rebind()而不是DataBind()

According to Telerik, when you need to reassign the datasource of a RadGrid, you should be calling Rebind() . 根据Telerik的说法,当你需要重新分配RadGrid的数据源时,你应该调用Rebind() Check the 2nd section Misusing or Not Using NeedDataSource Event on this Telerik page here for more. 有关更多信息,请查看此Telerik页面上的第二部分滥用或不使用NeedDataSource事件

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

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