LinqServerModeDataSource 中的自定义列

发布于 2024-09-16 05:30:52 字数 1835 浏览 5 评论 0原文

我的数据库中有一个名为 User 的表,其中包含字段:

Id
FirstName
LastName
LoginName

现在我想使用 LinqServerModeDataSourceASPxGridView 中呈现此表。

我所做的是:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-09-23 05:30:52

我尝试重现此问题并看到此行为。为了能够使用此方法编辑数据,我建议您执行以下操作:

1) 处理 ASPxGridView.CellEditorIntitialize 事件并将 e.Editor.ReadOnly 属性设置为 false。这将允许最终用户修改 EditForm 编辑器中的数据;

2)处理ASPxGridView.RowUpdating(RowInserting)事件并手动更新数据。另外,您应该将 e.Cancel 参数设置为 true(以防止网格本身更新数据),并调用 GridView 的 CancelEdit 方法来关闭 EditForm。

我还应该提到,不需要从数据库中获取用户名的数据。这可以使用 ASPxGridView 的 CustomColumnDisplayText 事件处理程序来完成:

protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) {
    if(e.Column.FieldName == "") {
        object[] values = ASPxGridView1.GetRowValues(e.VisibleRowIndex, new string[] { "FirstName", "LastName" }) as object[];
        e.DisplayText = values[0].ToString() + " " + values[1].ToString();
    }
}

如果此方法适合您,您可以避免使用 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:

protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) {
    if(e.Column.FieldName == "") {
        object[] values = ASPxGridView1.GetRowValues(e.VisibleRowIndex, new string[] { "FirstName", "LastName" }) as object[];
        e.DisplayText = values[0].ToString() + " " + values[1].ToString();
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文