Hi, 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?
Yes, you can nest GridViews inside one another. See the following article.
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.
I've never seen a page like that load in any reasonable amount of time. Especially if it has to make a db connection for every child grid. You won't be able to make the child grid appear beneath the parent row, it would have to be placed inside of a cell of the parent. It's pretty non trivial to change the parent GridView to be a Repeater or a DataList though. That gives you a bit more control over the html. Consider using an Ajax approach that will fetch the child grid on demand...like when the parent row is clicked. You can then have that row expand to show the recently fetched child data.
-mod me down if i'm too presumptuous. i'll completely understand.
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
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 ;)