使用多个表作为数据源时如何编辑 Telerik RadGrid
我在 Telerik 论坛上问过这个问题,但没有得到回复,所以我向你求助。
我以前见过这个话题,但没有答案,所以我想我应该尽可能简单地定义问题。
我有一个创建查询的 LINQ 命令:
var gridData = from au in dc.aspnet_Users 在 dc.aspnet_UsersInRoles 中加入 aur
au.UserId 等于 aur.UserId 将 ar 加入 dc.aspnet_Roles
aur.RoleId 等于 ar.RoleId 加入 dc.aspnet_Memberships
au.UserId 等于 am.UserId 选择新的 { au.用户名, ar.角色名称, 上午,电子邮件, am.已批准, am.IsLockedOut };
重要的一点是这使用了不止一张表。
注意:这是使用标准 ASP.NET 成员资格框架访问角色和用户表。
然后我有一个简单的 RadGrid:
<MasterTableView EditMode="InPlace">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
最后,我将数据绑定到 Page_Load 中的网格,
rgUsers.AutoGenerateColumns = true;
rgUsers.DataSource = gridData.ToList();
rgUsers.DataBind();
rgUsers.EditIndexes.Add(1);
rgUsers.DataBind();
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = gridData.ToList();
GridView1.DataBind();
GridView1.EditIndex=1;
GridView1.DataBind();
这将正确显示 4 个表中的数据。
我添加了一个 GridView 来比较功能。
我还以编程方式将网格置于编辑模式,以确保这不仅仅是“编辑”按钮的问题。
但是,在编辑模式下,RadGrid 字段不可编辑。
我尝试过以下操作:
1) 1) 我可以使用允许编辑/更新的 LinqDataSource,但前提是 LINQ 查询中只有 1 个表。
2) 2) 我可以在数据集上使用 ObjectDataSource,但这仅在数据集适配器访问单个表时才有效。
我查看了各种建议的演示和示例代码,但它们都忽略了多表/联接问题:
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
demos.telerik.com/aspnet-ajax/grid/examples/dataediting/
Threelevel /defaultcs.aspx www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
我是没有与 LINQ 结合。我很高兴能够进行调整,以获得可以支持更新和编辑多个表的 RadGrid。
我应该怎么做才能实现 RadGrid 显示由多个表组成的记录,并提供更新和编辑?
感谢您的帮助 理查德
I have asked this question on Telerik forums with no reply so I'm turing to you.
I have seen this topic come up before but without an answer so I thought I'd define the problem as simply as possible.
I have a LINQ command creating a query:
var gridData = from au in dc.aspnet_Users
join aur in dc.aspnet_UsersInRoles
on au.UserId equals aur.UserId
join ar in dc.aspnet_Roles
on aur.RoleId equals ar.RoleId
join am in dc.aspnet_Memberships
on au.UserId equals am.UserId
select new
{
au.UserName,
ar.RoleName,
am.Email,
am.IsApproved,
am.IsLockedOut
};
THE IMPORTANT POINT IS THAT THIS USES MORE THAN 1 TABLE.
Note: This is accessing the Roles and Users tables using the standard ASP.NET Membership framework.
Then I have a simple RadGrid:
<MasterTableView EditMode="InPlace">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
Finally I bind the data to the grid in Page_Load
rgUsers.AutoGenerateColumns = true;
rgUsers.DataSource = gridData.ToList();
rgUsers.DataBind();
rgUsers.EditIndexes.Add(1);
rgUsers.DataBind();
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = gridData.ToList();
GridView1.DataBind();
GridView1.EditIndex=1;
GridView1.DataBind();
This will display the data from the 4 tables correctly.
I included a GridView to compare functionality.
I also put the grid into Edit mode programatically to make sure that this was not just a problem with the Edit button.
However, when in Edit mode the RadGrid fields are not editable.
I have tried the following:
1) 1) I can use a LinqDataSource that allows Edit/Update BUT only if there is only 1 table in the LINQ query.
2) 2) I can use a ObjectDataSource onto a dataset but once again this only works if the the dataset adaptor is accessing a single table.
I have looked at the variouse suggested demos and example code but they have all ignored the multiple table/Join problem:
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
demos.telerik.com/aspnet-ajax/grid/examples/dataediting/threelevel/defaultcs.aspx
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
I am not wedded to LINQ. I am quite happy to adapt in order to get a RadGrid that can support Update and Edit to multiple tables.
What should I do to achieve a RadGrid that displays records composed from multiple tables with Update and Edit available?
Thanks for any help
Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过将 ADO.net 的 dataview 类与数据集类结合使用?如果您正在使用的数据库类型,您应该能够使用带有存储过程的 system.data.sqlclient 类来提取数据。您可以在数据集类中保留数据库的关系,并且数据视图类将允许您像查看单个表一样查看数据。然后您应该能够将 dataview 类绑定到 Telerik 控件。
Have you considered using ADO.net's dataview class in conjunction with a dataset class? You should be able to pull you data using the system.data.sqlclient class with a stored proc if that is the type of database you're using. You can preserve your database's relationships in the dataset class and the dataview class will allow you to view the data as if it were a single table. Then you should be able to bind the dataview class to the Telerik control.
为什么不使用 NeedDataSource 绑定而不是 DataBind() 调用?因此,Telerik 网格应该管理其编辑状态,而无需额外编码。如果您对不同的源表使用联接,并使用适当的 T-SQL 语句设置更新或插入命令,则一切都应该正常工作。首先查看此演示:
http:// demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
迪克
Why not using NeedDataSource binding as opposed to DataBind() calls? Thus the Telerik grid should manage its editing states without extra coding. If you use joins for different source tables and set you update or insert commands using appropriate T-SQL statements, everything should work fine. Check out this demo for a start:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
Dick
事实证明,LINQ 查询返回只读的匿名类型。
我使用存储过程将其替换为 SQLDataSource,因此它现在可以工作了。
谢谢
Turns out that the LINQ query returns an anonymous type that is read only.
I replaced it with a SQLDataSource using stored procedures so it works now.
Thanks