使用 asp:LinqDataSource 的动态列

发布于 2024-09-15 10:39:25 字数 1813 浏览 3 评论 0原文

我像这样使用 LinqDataSource:

<asp:LinqDataSource ID="LinqDataSource3" runat="server"      OnSelecting="LinqDataSource3_OnSelecting">       

</asp:LinqDataSource>

并且我有 ASPxGridView

<dxwgv:ASPxGridView ID="ASPxGridView2" ClientInstanceName="ASPxGridView2Client"
                            runat="server" AutoGenerateColumns="False" 
                            DataSourceID="LinqDataSource3">
                        </dxwgv:ASPxGridView>

以这种方式,我能够在处理 onSelecting 事件时动态添加列:

protected void LinqDataSource3_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        MyModelDataContext context = new MyModelDataContext();
        ASPxGridView grid = ASPxPageControl1.TabPages[3].FindControl("ASPxGridView2") as ASPxGridView;


        if(grid.Columns.Count == 0){
            GridViewDataTextColumn txtColumn1 = new GridViewDataTextColumn();
            GridViewDataTextColumn txtColumn2 = new GridViewDataTextColumn();
            txtColumn1.FieldName = "UserId";
            txtColumn2.FieldName = "FirstName";

            grid.Columns.Add(txtColumn1);
            grid.Columns.Add(txtColumn2);    
        }

        e.Result = from u in context.Users select new { UserId = u.UserId, FirstName = u.FirstName };
    } 

这一切都是为了测试,因为我的想法是找到一个解决方案,使得可以使用 linqDataSource 作为 de 的数据源ASPxGridView 但连接 2 或 3 个表并在此网格中显示结果。

我想问的是,它是否是一个好的解决方案,或者是否有其他更好的方法来呈现通过联接从几个表中生成的一些视图?

第二个问题是我有用户和组,我想要一个表,其中有两列名称和类型

对于组类型将按组,对于用户类型将是用户。

我的数据库中没有这样的表

是否可以创建特定的类。创建该类的对象列表并使用 linq 查询填充它,并将其用作该网格的 linq 数据源?

clas 将是:

伪代码:

Class MyClass {
string Type;
string Name;
}

或者还有其他方法来创建这样的表吗?

非常感谢你的帮助再见

Im using LinqDataSource like that:

<asp:LinqDataSource ID="LinqDataSource3" runat="server"      OnSelecting="LinqDataSource3_OnSelecting">       

</asp:LinqDataSource>

And I have ASPxGridView

<dxwgv:ASPxGridView ID="ASPxGridView2" ClientInstanceName="ASPxGridView2Client"
                            runat="server" AutoGenerateColumns="False" 
                            DataSourceID="LinqDataSource3">
                        </dxwgv:ASPxGridView>

In this way Im able to add columns dynamically while handling onSelecting event:

protected void LinqDataSource3_OnSelecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        MyModelDataContext context = new MyModelDataContext();
        ASPxGridView grid = ASPxPageControl1.TabPages[3].FindControl("ASPxGridView2") as ASPxGridView;


        if(grid.Columns.Count == 0){
            GridViewDataTextColumn txtColumn1 = new GridViewDataTextColumn();
            GridViewDataTextColumn txtColumn2 = new GridViewDataTextColumn();
            txtColumn1.FieldName = "UserId";
            txtColumn2.FieldName = "FirstName";

            grid.Columns.Add(txtColumn1);
            grid.Columns.Add(txtColumn2);    
        }

        e.Result = from u in context.Users select new { UserId = u.UserId, FirstName = u.FirstName };
    } 

It is all made simply to test cause my idea is to have solution that makes it possible to use linqDataSource as a datasource for de ASPxGridView but join 2 or 3 tables and show results in this grid.

What I want to ask is if its good solution or is there any other better way to present some views made from couple of tables by join?

Second question is that I have Users and Group and I would like to have one table in which I would have two columns Name and Type

For groups type would by group and for users type would be user.

I dont have such a table in my database

Is it possible to create specific class. Create List of objects of that class and fill it using linq query and the use it as linq data source for that grid ?

clas would be:

pseudocode:

Class MyClass {
string Type;
string Name;
}

or is there any other way to create such a table ?

thank You very much for help bye

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

祁梦 2024-09-22 10:39:25

回答你的第一个问题,我想告诉你,创建列的代码最好移至 Page_Load 事件。

至于你的第二个问题。我认为在 LinqDataSource 对象的 Selecting 事件中提供自定义 IQueriable 是一个很好的解决方案。您可以从那里的多个表中获取数据。有关示例,请参阅:

使用 LINQ 从多个表中选择

Answering your first question, I would like to tell you that the code to create columns is better to move to the Page_Load event.

As for your second question. I think that it is a good solution to provide a custom IQueriable within the Selecting event of the LinqDataSource object. You may fetch data from several tables there. For an example, please refer to:

Select from multiple table using LINQ

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