ASP.NET:GridView 可用于创建层次结构吗?

发布于 2024-07-12 03:02:29 字数 114 浏览 7 评论 0原文

我们在项目的某些页面中使用了 GridView 控件,但我们不想对其进行大幅更改,是否可以在 gridview 中创建层次结构? 这可以通过在 Gridview 中使用 GridView 来获取父子关系来实现吗?

We are using the GridView controls in some pages of our project which we dont want to change drastically, would it be possible to create a hierarchy in a gridview? Can this be achieved by using a GridView inside a Gridview to get the parent - child relation?

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

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

发布评论

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

评论(5

心清如水 2024-07-19 03:02:29

是的,你可以,而且很简单...

最好的方法是拥有一些 ObjectDataSource,以便整个过程对你来说更容易,或者当然,你可以在父 gridview OnRowDataBound 事件中绑定嵌套的 gridview,这就是全部取决于你:)

示例:

<asp:GridView ID="gvGrandFather" runat="server" DataSourceID="odsGrandFather">
    <Columns>
        <asp:BoundField DataField="myField1" HeaderText="myText1" />
        <asp:BoundField DataField="myField2" HeaderText="myText2" />
        <asp:BoundField DataField="myField3" HeaderText="myText3" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="gvFather" runat="server" DataSourceID="odsFather">
                    <Columns>
                        <asp:BoundField DataField="myField1" HeaderText="myText1" />
                        <asp:BoundField DataField="myField2" HeaderText="myText2" />
                        <asp:BoundField DataField="myField3" HeaderText="myText3" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:GridView ID="gvSon" runat="server" DataSourceID="odsSon">
                                    <Columns>
                                        <asp:BoundField DataField="myField1" HeaderText="myText1" />
                                        <asp:BoundField DataField="myField2" HeaderText="myText2" />
                                        <asp:BoundField DataField="myField3" HeaderText="myText3" />
                                    </Columns>
                                </asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="odsGrandFather" runat="server" DataObjectTypeName="Company" TypeName="CompanyDAO" SelectMethod="FindAll" />
<asp:ObjectDataSource ID="odsFather" runat="server" DataObjectTypeName="Employees" TypeName="EmployeesDAO" SelectMethod="FindByID">
    <SelectParameters>
        <asp:Parameter Name="myFieldInCompanyObject" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="odsSon" runat="server" DataObjectTypeName="Person"TypeName="PersonsDAO" SelectMethod="FindByID">
    <SelectParameters>
        <asp:Parameter Name="myFieldInEmployeesObject" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

假设你有你的 Company 对象,就像

Company
  Field1
  Field2
  Field3
  Employees witch is List<Person>
    Field1
    Field2
    Field3
    Person  witch is List<Person>
      Field1
      Field2
      Field3

你需要做的就是每个对象的 DAO 并返回列表或对象本身,就像

public class CompanyDAO
{
    private List<Company> Companies
    {
        get
        {
            List<Company> companies = HttpContext.Current.Session["Companies"] as List<Company>;
            if (companies == null)
                companies = new List<Company>();
            return companies;
        }
    }
    public CompanyDAO() { }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<Company> FindAll()
    {
        return this.Companies;
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<Company> FindByID(String CompanyID)
    {
        return (from c in this.Companies where c.ID == CompanyID select c).ToList();
    }
}

希望它有助于看到隧道尽头的光明;)

yes you can, and it's quite easy...

the best approach is to have some ObjectDataSource's in order to the entire process be easier for you, or off course, you can bind the nasted gridview in the paraent gridview OnRowDataBound event, it is all up to you :)

example:

<asp:GridView ID="gvGrandFather" runat="server" DataSourceID="odsGrandFather">
    <Columns>
        <asp:BoundField DataField="myField1" HeaderText="myText1" />
        <asp:BoundField DataField="myField2" HeaderText="myText2" />
        <asp:BoundField DataField="myField3" HeaderText="myText3" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="gvFather" runat="server" DataSourceID="odsFather">
                    <Columns>
                        <asp:BoundField DataField="myField1" HeaderText="myText1" />
                        <asp:BoundField DataField="myField2" HeaderText="myText2" />
                        <asp:BoundField DataField="myField3" HeaderText="myText3" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:GridView ID="gvSon" runat="server" DataSourceID="odsSon">
                                    <Columns>
                                        <asp:BoundField DataField="myField1" HeaderText="myText1" />
                                        <asp:BoundField DataField="myField2" HeaderText="myText2" />
                                        <asp:BoundField DataField="myField3" HeaderText="myText3" />
                                    </Columns>
                                </asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="odsGrandFather" runat="server" DataObjectTypeName="Company" TypeName="CompanyDAO" SelectMethod="FindAll" />
<asp:ObjectDataSource ID="odsFather" runat="server" DataObjectTypeName="Employees" TypeName="EmployeesDAO" SelectMethod="FindByID">
    <SelectParameters>
        <asp:Parameter Name="myFieldInCompanyObject" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="odsSon" runat="server" DataObjectTypeName="Person"TypeName="PersonsDAO" SelectMethod="FindByID">
    <SelectParameters>
        <asp:Parameter Name="myFieldInEmployeesObject" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

imagine that you have your Company object like

Company
  Field1
  Field2
  Field3
  Employees witch is List<Person>
    Field1
    Field2
    Field3
    Person  witch is List<Person>
      Field1
      Field2
      Field3

All you need to do is the DAO for each and return the list or the object itself like

public class CompanyDAO
{
    private List<Company> Companies
    {
        get
        {
            List<Company> companies = HttpContext.Current.Session["Companies"] as List<Company>;
            if (companies == null)
                companies = new List<Company>();
            return companies;
        }
    }
    public CompanyDAO() { }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<Company> FindAll()
    {
        return this.Companies;
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IEnumerable<Company> FindByID(String CompanyID)
    {
        return (from c in this.Companies where c.ID == CompanyID select c).ToList();
    }
}

hope it helps see the light at the end of the tunnel ;)

紧拥背影 2024-07-19 03:02:29

是的,您可以将 GridView 嵌套在一起。 请参阅以下文章。

嵌套 GridView

Yes, you can nest GridViews inside one another. See the following article.

Nesting GridViews

故事未完 2024-07-19 03:02:29

看看 Telerik,他们的 RadGrid 有这种开箱即用的东西(NestedHierarchy 和详细表)

http://demos.telerik.com/aspnet-ajax/Grid/Examples/Overview/DefaultCS.aspx

Check out Telerik, their RadGrid has this kind of stuff out of the box (NestedHierarchy and detailtables)

http://demos.telerik.com/aspnet-ajax/Grid/Examples/Overview/DefaultCS.aspx

澉约 2024-07-19 03:02:29

这方面的好文章
这里
它使用 ASP.NET AJAX 和 AJAX Control Toolkit 使其可折叠。

Good articles on this
here
which uses ASP.NET AJAX and the AJAX Control Toolkit to make it collapsible.

尐籹人 2024-07-19 03:02:29

GridView 是相当静态的。 如果表变得更加复杂,我们就使用 XSLT,这样我们就可以对其进行最终控制。 另外,Microsoft 还提供其他 ASP.NET 控件,例如 DataRepeater。

GridView's are quite static. We use XSLT if the tables are going to be more complex so we have ultimate control over it. Otherwise Microsoft provide other ASP.NET controls such as the DataRepeater.

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