DataGrid嵌套绑定
一个DataGrid中绑定父表,然后建一个模板列,里面嵌入另一个 DataGrid绑定子表,另外定义一个加号图片的Click的事件

解决方案 »

  1.   

    .aspx中
    <asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 128px" runat="server"
    Width="960px" AutoGenerateColumns="False" BorderColor="Blue">
    <Columns>
    <asp:TemplateColumn HeaderText="主题(点击可展开列表)">
    <HeaderStyle Width="450px"></HeaderStyle>
    <ItemTemplate>
    <asp:Label ID="lbID" Runat ="server" Visible = "False" Text='<%# DataBinder.Eval(Container,"DataItem.id")%>'>
    </asp:Label>
    <asp:ImageButton runat="server" Width="15" Height="15" ID="ImgOpen" CommandName="data"></asp:ImageButton>
    <%# "<a href='showArt.aspx?id="+DataBinder.Eval(Container,"DataItem.art_ans_id")+"&main_type_style="+DataBinder.Eval(Container,"DataItem.main_type_style")+"&title="+DataBinder.Eval(Container,"DataItem.id")+"'>"%>
    <asp:Label ID="lbTitle" Runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.art_title")%>' >
    </asp:Label>
    <%# "</a>" %>
    <br>
    &nbsp;&nbsp;
    <asp:DataList Runat="server" ID="dataList1" 
    GridLines="Both" BorderStyle="Groove" Width="300"

    RepeatColumns="1" Visible="False" RepeatLayout="Flow">
    <ItemTemplate>
    &nbsp;&nbsp;
    <%# "<a href='showArt.aspx?id="+DataBinder.Eval(Container,"DataItem.art_ans_id")+"'>"%>
    <asp:Image ID="imgCh" Runat="server" Width="15" Height="15" ImageUrl="pic/Sub.ico"></asp:Image>
    <asp:Label id="lbChTitle" Runat ="server" Text='<%# DataBinder.Eval(Container,"DataItem.art_title")%>'>
    </asp:Label>
    <%# "</a>"%>
    </ItemTemplate>
    </asp:DataList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="art_state" HeaderText="状态">
    <HeaderStyle Width="60px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="us_account" HeaderText="作者">
    <HeaderStyle Width="72px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="回复/人气">
    <HeaderStyle Width="72px"></HeaderStyle>
    <ItemTemplate>
    <%# DataBinder.Eval(Container,"DataItem.couRep")%>
    /
    <%# DataBinder.Eval(Container,"DataItem.welcome")%>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="最后更新/回复人">
    <ItemTemplate>
    <%# DataBinder.Eval(Container,"DataItem.art_input_time")%>
    /
    <%# DataBinder.Eval(Container,"DataItem.us_account")%>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
    </td>
    <td width="60px" style="BORDER-RIGHT: #cccc99 1px solid">
    <%# DataBinder.Eval(Container,"DataItem.topic") %>
    </td>
    <td width="60px" style="BORDER-RIGHT: #cccc99 1px solid">
    <%# DataBinder.Eval(Container,"DataItem.reply") %>
    </td>
    <td width="190px">
    主题:<%# DataBinder.Eval(Container,"DataItem.LastTopic")%><br>
    最后回复:<%# DataBinder.Eval(Container,"DataItem.LastReply")%><br>
    回复时间:<%# DataBinder.Eval(Container,"DataItem.LastTime")%>
    </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:DataList>
    </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
      

  2.   

    在.cs中
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    BindData();
    for(int i = 0; i < DataGrid1.Items.Count; i ++)
    {
    ImageButton ImgOpen=(ImageButton)DataGrid1.Items[i].FindControl("ImgOpen");
    ImgOpen.ImageUrl = "pic/Add.ico";
    }
    lbBBS.Text = "<a href ='main.aspx'>BBS首页</a>";
    lbThis.Text = "<a href ='showlist.aspx'>"+Request.QueryString["main_type_style"].ToString()+"</a>";
    InitDataList();
    }
    }private void InitDataList()
    {
    for(int i = 0; i < DataGrid1.Items.Count; i ++)
    {
    DataList dataL=(DataList)DataGrid1.Items[i].FindControl("dataList1");
    dataL.Visible=false;
    }
    }private void BindData()
    {
    DataSet ds = new DataSet();

    SqlConnection nwindConn = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString()); 
    string strSelPar = "select *, dbo.countRep(artical.[id]) as couRep ,dbo.welcome(artical.[id]) as welcome,dbo.LastrepTitle(artical.[id]) as lastRep,"
    +"dbo.LastRepAccount(artical.[id]) as LastAccount from artical where [id] = art_ans_id and "
    +"main_type_style = '"+Request.QueryString["main_type_style"]+"' order by [id] desc";
    string strSelChild = "select * from artical where [id] != art_ans_id and main_type_style = '"
    +Request.QueryString["main_type_style"]+"' order by [id] desc";
    SqlDataAdapter daParent = new SqlDataAdapter(strSelPar, nwindConn);
    SqlDataAdapter daChild = new SqlDataAdapter(strSelChild, nwindConn);

    try
    {
    daParent.Fill(ds,"Parent");
    daChild.Fill(ds,"Child");
    ds.Relations.Add("myrelation", ds.Tables["Parent"].Columns["id"], ds.Tables["Child"].Columns["art_ans_id"]);

    DataGrid1.DataSource=ds.Tables["Parent"].DefaultView;
    DataBind();
    }
    catch(Exception en)
    {
    throw en;
    }
    finally
    {
    daParent.Dispose();
    daChild.Dispose();
    nwindConn.Close();
    }
    }private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    ImageButton ImgOpen=(ImageButton)e.Item.FindControl("ImgOpen");
    Label Id=(Label)e.Item.FindControl("lbID");
    DataList dataL=(DataList)e.Item.FindControl("dataList1");
    if(e.CommandName =="data")
    {
    if(dataL.Visible == true)
    {
    ImgOpen.ImageUrl="Pic/Add.ICO";
    dataL.Visible=false;
    }
    else
    {
    ImgOpen.ImageUrl="Pic/Sub.ico";
    dataL.Visible = true;
    ConnectData cd = new ConnectData();
    string Sql="select * from artical where art_ans_id="+Id.Text+" and [id] != art_ans_id order by [id] desc";
    DataTable dt = new DataTable();
    cd.ReadTb(Sql,dt);
    dataL.DataSource = dt.DefaultView;
    dataL.DataBind();
    for(int i=0;i<dataL.Items.Count;i++)
    {
    Label Child=(Label)dataL.Items[i].FindControl("lbChTitle");
    Child.Visible = true;
    }
    }
    }
    Label lbTitle = (Label)e.Item.FindControl("lbTitle");      
    }