Question

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?

Was it helpful?

Solution

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

Nesting GridViews

OTHER TIPS

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 ;)

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top