我们在项目的某些页面中使用GridView控件,我们不想大幅改变,是否可以在gridview中创建层次结构?这可以通过在Gridview中使用GridView来获得父子关系来实现吗?
答案 0 :(得分:5)
是的,你可以,而且很容易......
最好的方法是拥有一些ObjectDataSource,以便整个过程更容易,或者当然,你可以在paraent gridview OnRowDataBound事件中绑定nasted 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
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();
}
}
希望它能帮助看到隧道尽头的灯光;)
答案 1 :(得分:0)
是的,您可以将GridViews嵌套在一起。请参阅以下文章。
答案 2 :(得分:0)
查看Telerik,他们的RadGrid有开箱即用的这种东西(NestedHierarchy和detailtables)
http://demos.telerik.com/aspnet-ajax/Grid/Examples/Overview/DefaultCS.aspx
答案 3 :(得分:0)
关于此的好文章 here 它使用ASP.NET AJAX和AJAX Control Toolkit使其可折叠。
答案 4 :(得分:-1)
GridView是非常静态的。如果表格会更复杂,我们会使用XSLT,因此我们可以对它进行最终控制。否则Microsoft提供其他ASP.NET控件,例如DataRepeater。