see an example here for repeaters, but the idea is same:HOW TO: Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

解决方案 »

  1.   

    我也赞成用Reperter,否则要么就只列一BoundColumn,然后在列里布局..
    一个非常傻的做法:<%@ Import namespace="System.Xml" %>
    <%@ Import namespace="System.Web.UI.WebControls" %>
    <%@ Import namespace="System.Web.UI" %>
    <%@ Import namespace="System.Web" %>
    <%@ Import namespace="System.Data.SqlClient" %>
    <%@ Import namespace="System.Data" %>
    <%@ Import namespace="System" %>
    <%@ Page language="c#" Debug="false" AutoEventWireup="false" Inherits="System.Web.UI.Page" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Test</title>
    <script language="C#" runat="server">
    protected override void OnLoad(EventArgs e)
    {
    if(!IsPostBack)
    {
    BindDataGrid();
    }
    }
    protected void BindDataGrid()
    {
    DataSet ds=new DataSet();
    using(SqlConnection conn=new SqlConnection("Server=(local);UID=sa;PWD=xxxx;Initial Catalog=xxxx"))
    {
    conn.Open();
    using(SqlCommand cmd=new SqlCommand("SELECT * FROM Departments Inner Join Employees ON Employees.DepartmentID=Departments.DepartmentID FOR XML AUTO,XMLDATA",conn))
    {
    XmlReader xr=cmd.ExecuteXmlReader();
    ds.ReadXml(xr,XmlReadMode.Fragment);
    xr.Close();
    DataGrid1.DataSource=ds.Tables["Departments"].DefaultView;
    DataGrid1.DataBind();
    }
    }
    }
    protected DataView GetDataView(object DataItem)
    {
    DataView view=new DataView(((DataRowView)DataItem).DataView.Table.ChildRelations[0].ChildTable);
    view.RowFilter="DepartmentID="+DataBinder.Eval(DataItem,"DepartmentID");
    return view;
    }
    </script>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Test" method="post" runat="server">
    <P>
    <asp:DataGrid id="DataGrid1" runat="server" Width="339px" BorderStyle="None" BorderWidth="1px" BorderColor="#CCCCCC" BackColor="White" CellPadding="3">
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
    <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
    <ItemStyle ForeColor="#000066"></ItemStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="DataGrid" FooterText="DataGrid">
    <ItemTemplate>
    <asp:DataGrid id=DataGrid2 runat="server" CellPadding="3" BackColor="White" BorderColor="White" BorderWidth="2px" BorderStyle="Ridge" Width="340px" CellSpacing="1" GridLines="None"
    DataSource="<%# GetDataView(Container.DataItem) %>">
    <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
    <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
    <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
    <Columns>
    <asp:BoundColumn DataField="EmployeeName" HeaderText="EmployeeName" FooterText="EmployeeName"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </P>
    </form>
    </body>
    </HTML>
      

  2.   

    to:saucer(思归) 
    谢谢,可是不用DATASET
    我用OleDbDataReader又该什么做呢?
      

  3.   

    to saucer(思归):
    那个例子我看了一下,关键在于使用这样的数据绑定表达式:
    <%#((DataRowView)Container.DataItem).Row.GetChildRows("myrelation")%>该绑定会返回一个DataRowCollection集合,可是如果我并不想取出ChildRows中的所有行,而只是想取出其中某个column满足某个值的行,又该怎么办呢?DataTable有select,Dataview有RowFilter,可DataRowCollection只有个Find by id!
      

  4.   

    write a custom methodDataRows[] GetRows(DataRow row)
    {
    //get row's child rows and filter it
    }//.....
    <%# GetRows(((DataRowView)Container.DataItem).Row)%>