应该从数据库取出的时候就作处理。
然后再databind的时候过滤掉重复的数据。。

解决方案 »

  1.   

    take a look at these examples to display Master/Detail informationHOW TO: Create a Master/Detail Page with Web Form Controls
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308485Vanishing Master/Detail DataGrid
    http://www.dotnetjunkies.net/tutorials.aspx?tutorialid=387or play with this simple nested datagrids:<%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %><html><body>  <h3><font face="Verdana">Nested Grid</font></h3>
    <form runat="server">
      <ASP:DataGrid id="MyDataGrid" runat="server"
        Width="700"
        ForeColor="#ff0000"
        BackColor="" 
        BorderColor="black"
        ShowFooter="false" 
        CellPadding=3 
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        EnableViewState="false"
        OnItemDataBound="Customer_OnItemDataBound"
    /></form></body>
    </html>
    <script language="C#" runat="server">    SqlConnection myConnection;    protected void Page_Load(Object sender, EventArgs e) 
        {
            myConnection = new SqlConnection("server=localhost;database=NorthWind;uid=sa;pwd=;");
            SqlDataAdapter myCommand = new SqlDataAdapter("select CustomerID, CompanyName ContactName from Customers", myConnection);        DataSet ds = new DataSet();
            myCommand.Fill(ds, "Customers"); DataView dv = ds.Tables["Customers"].DefaultView;
            MyDataGrid.DataSource= dv;        MyDataGrid.DataBind();    }
       protected void Customer_OnItemDataBound(object sender, DataGridItemEventArgs e)
       {
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
           {
    String CustomerID = DataBinder.Eval(e.Item.DataItem, "CustomerID").ToString();

    DataGrid dg = new DataGrid(); SqlDataAdapter myCommand = new SqlDataAdapter("select OrderID, OrderDate from orders WHERE CustomerID = '" + CustomerID + "'" , myConnection);         DataSet ds = new DataSet();
             myCommand.Fill(ds, "Orders"); DataView dv = ds.Tables["Orders"].DefaultView;
             dg.DataSource= dv;
             dg.DataBind(); TableCell cell = new TableCell();
    cell.Controls.Add(dg);
    e.Item.Cells.Add(cell);
    }
       }</script>
      

  2.   

    谢谢saucer(思归),
    DataGrid有OnItemDataBound事件可以在创建时绑定子表,
    有没有使用repeater的例子?
    下面这段代码可以绑定子表,但是我运行时总是说服务器标记错误,错误在这一行:
    datasource="<%# ((DataRowView)Container.DataItem)
          .Row.GetChildRows("myrelation") %>" runat="server">
    能否帮我看看是哪里错了。Nestedrepeater.aspx 
    <%@ Page Language=C# Inherits="yourprojectname.nestedrepeater" %>
    <%@ Import Namespace="System.Data" %>
    <html>
    <body>
    <form runat=server>
    <!-- start parent repeater -->
    <asp:repeater id="parent" runat="server">
       <itemtemplate>
          <b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br>
          <!-- start child repeater -->
          <asp:repeater id="child" datasource="<%# ((DataRowView)Container.DataItem)
          .Row.GetChildRows("myrelation") %>" runat="server">
             <itemtemplate>
                <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><br>
             </itemtemplate>
          </asp:repeater>
          <!-- end child repeater -->
       </itemtemplate>
    </asp:repeater>
    <!-- end parent repeater -->
    </form>
    </body>
    </html>
    Nestedrepeater.aspx.cs 
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace yourprojectname
    {
       public class nestedrepeater : System.Web.UI.Page
       {
          protected System.Web.UI.WebControls.Repeater parent;
          public nestedrepeater()
          {
             Page.Init += new System.EventHandler(Page_Init);
          }
          public void Page_Load(object sender, EventArgs e)
          {
             //Create the connection and DataAdapter for the Authors table.
             SqlConnection cnn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=;");
             SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);         //Create and fill the DataSet.
             DataSet ds = new DataSet();
             cmd1.Fill(ds,"authors");         //Create a second DataAdapter for the Titles table.
             SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
             cmd2.Fill(ds,"titles");         //Create the relation bewtween the Authors and Titles tables.
             ds.Relations.Add("myrelation",
             ds.Tables["authors"].Columns["au_id"],
             ds.Tables["titles"].Columns["au_id"]);         //Bind the Authors table to the parent Repeater control, and call DataBind.
             parent.DataSource = ds.Tables["authors"];
             Page.DataBind();         //Close the connection.
             cnn.Close();
          }
          private void Page_Init(object sender, EventArgs e)
          {
             InitializeComponent();
          }
          private void InitializeComponent()
          {    
             this.Load += new System.EventHandler(this.Page_Load);
          }
       }
    }
      

  3.   

    since you already have " inside, you need to use ':<asp:repeater id="child" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">