LinqServerModeDataSource 中的自定义列
我的数据库中有一个名为 User 的表,其中包含字段:
Id
FirstName
LastName
LoginName
现在我想使用 LinqServerModeDataSource
在 ASPxGridView
中呈现此表。
我所做的是:
<dxdtlnq:LinqServerModeDataSource ID="LinqServerModeDataSource1" runat="server" OnSelecting="LinqServerModeDataSource1_OnSelecting"
ContextTypeName="MyContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" TableName="Users" >
</dxdtlnq:LinqServerModeDataSource>
protected void LinqServerModeDataSource1_OnSelecting(object sender, LinqServerModeDataSourceSelectEventArgs e)
{
MyContext context = new MyContext();
var qry = from s in context.Users select s;
e.QueryableSource = qry;
}
这与我的 ASPxGridView 配合得很好。我可以显示数据、插入和删除,但现在我想要附加列 UserName
,即 FirstName
+ LastName
。
所以我做了类似的事情:
向我的 ASPxGridView 添加了适当的列(FieldName =“UserName”) 并修改了 OnSelecting 处理程序:
protected void LinqServerModeDataSource1_OnSelecting(object sender, LinqServerModeDataSourceSelectEventArgs e) {
KozuModelDataContext context = new MyContext();
var qry = (from s in context.Substitutions
select new
{
Id = s.Id,
FirstName = s.FirstName,
LastName = s.LastName,
LoginName = s.LoginName,
UserName = s.FirstName + " " + s.LastName,
}).AsQueryable();
e.KeyExpression = "Id";
e.QueryableSource = qry;
}
现在,当我想要插入或编辑数据字段时,网格中的数据将显示为无法填充,文本框在插入表单中没有响应,我无法键入任何文本。
有没有以这种方式插入和编辑数据的解决方案?
感谢您的帮助
I have table in my database names User with fields:
Id
FirstName
LastName
LoginName
Now I would like to present this table in ASPxGridView
using LinqServerModeDataSource
.
What I did is :
<dxdtlnq:LinqServerModeDataSource ID="LinqServerModeDataSource1" runat="server" OnSelecting="LinqServerModeDataSource1_OnSelecting"
ContextTypeName="MyContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" TableName="Users" >
</dxdtlnq:LinqServerModeDataSource>
protected void LinqServerModeDataSource1_OnSelecting(object sender, LinqServerModeDataSourceSelectEventArgs e)
{
MyContext context = new MyContext();
var qry = from s in context.Users select s;
e.QueryableSource = qry;
}
That works great with my ASPxGridView. I can display data, insert and delete but now I would like to have additional column UserName
which is FirstName
+ LastName
.
So I did something like that:
Added appropriate column to my ASPxGridView (FieldName = "UserName")
and modified OnSelecting handler:
protected void LinqServerModeDataSource1_OnSelecting(object sender, LinqServerModeDataSourceSelectEventArgs e) {
KozuModelDataContext context = new MyContext();
var qry = (from s in context.Substitutions
select new
{
Id = s.Id,
FirstName = s.FirstName,
LastName = s.LastName,
LoginName = s.LoginName,
UserName = s.FirstName + " " + s.LastName,
}).AsQueryable();
e.KeyExpression = "Id";
e.QueryableSource = qry;
}
Now data in the grid is displayed buyt when I want to insert or edit data fields cannot be filled, textboxes doesnt respond in inserting form I cant type in any text.
Is there any solution for inserting and editing data in this manner ?
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试重现此问题并看到此行为。为了能够使用此方法编辑数据,我建议您执行以下操作:
1) 处理 ASPxGridView.CellEditorIntitialize 事件并将 e.Editor.ReadOnly 属性设置为 false。这将允许最终用户修改 EditForm 编辑器中的数据;
2)处理ASPxGridView.RowUpdating(RowInserting)事件并手动更新数据。另外,您应该将 e.Cancel 参数设置为 true(以防止网格本身更新数据),并调用 GridView 的 CancelEdit 方法来关闭 EditForm。
我还应该提到,不需要从数据库中获取用户名的数据。这可以使用 ASPxGridView 的 CustomColumnDisplayText 事件处理程序来完成:
如果此方法适合您,您可以避免使用 Selecting 事件,从而设置 LinqServerModeDataSource 的 TableName。这将允许您提供数据编辑功能,而不使用我上面解释的方法。
I have tried to reproduce this issue and see this behavior. To be able to edit data using this approach, I suggest that you do the following:
1) Handle the ASPxGridView.CellEditorIntitialize event and set the e.Editor.ReadOnly property to false. This will allow the end-user to modify data in the EditForm editors;
2) Handle the ASPxGridView.RowUpdating (RowInserting) event and update data manually. Also, you should set the e.Cancel parameter to true (to prevent the grid from updating data itself) and also call the GridView's CancelEdit method to close the EditForm.
I should also mention that there is no need to fetch data for the UserName from the DB. This can be done using the ASPxGridView's CustomColumnDisplayText event handler:
If this approach works for you, you can avoid using the Selecting event and thus set the LinqServerModeDataSource's TableName. This will allow you to provide the data editing feature not using the approach I explained above.