gridview中嵌套gridview后如何实现如下:制作单号   顾客姓名      年龄(gridview1)
10001       张三          25制作单号    项目名称     规格(gridview1中的gridview2)
10001        相册        18寸
10001        像框        20寸制作单号   顾客姓名      年龄(gridview1)
10002       王五          29制作单号    项目名称     规格gridview1中的gridview2)10002        相册        12寸
10002        像框        22寸请问各位大侠,能否做到这样?如果能的话该怎么写?谢谢!!!急~~~~~~~~~

解决方案 »

  1.   

    请参考:
    http://blog.csdn.net/lucy198204/archive/2006/10/20/1342969.aspx
      

  2.   

       先绑定主表,然后根据选择的子表的主键值绑定子表。不知你要的是不是这样的效果   给你个例子,不过你要把例子中的detailview改成gridview 就行了
      ====== Code  ===============<%@ Page Language="C#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">    private void Page_Load(object sender, EventArgs e) {
        
            if (!Page.IsPostBack) {
                // Databind the master grid on the first request only
                // (viewstate will restore these values on subsequent postbacks).
        
                MasterGrid.SelectedIndex = 0;
                BindMasterGrid();
                BindDetailGrid();
            }
        }
        
        private void MasterGrid_SelectedIndexChanged(object sender, EventArgs e) {
            BindDetailGrid();
        }
        
        private void MasterGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e) {
            if (MasterGrid.SelectedIndex != -1) {
                // unset the selection, details view
                MasterGrid.SelectedIndex = -1;
                BindDetailGrid();
            }
        
            MasterGrid.CurrentPageIndex = e.NewPageIndex;
            BindMasterGrid();
        }
        
        private void BindMasterGrid() {
        
            // TODO: Update the ConnectionString and CommandText values for your application
            string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
            string CommandText = "select au_lname as [Last Name], au_fname as [First Name], Address, City, State from Authors order by [Last Name]";
        
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
        
            DataSet ds = new DataSet();
            myCommand.Fill(ds);
        
            MasterGrid.DataSource = ds;
            MasterGrid.DataBind();
        }
        
        private void BindDetailGrid() {
            // get the filter value from the master Grid's DataKeys collection
            if (MasterGrid.SelectedIndex != -1) {
                // TODO: update the ConnectionString value for your application
                string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
        
                // TODO: update the CommandText value for your application
                string filterValue = ((string)MasterGrid.DataKeys[MasterGrid.SelectedIndex]).Replace("'", "''");
                string CommandText = "select title as Title, price as Price, ytd_sales as [YTD Sales] from titleview where au_lname = '" + filterValue + "'";
        
                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
        
                myConnection.Open();
        
                DetailsGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
        
            DetailsGrid.DataBind();
        }</script>
    <html>
    <head>
    </head>
    <body style="FONT-FAMILY: arial">
        <h2>Master - Detail Grids 
        </h2>
        <hr size="1" />
        <form runat="server">
            <p>
                <asp:datagrid id="MasterGrid" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="MasterGrid_PageIndexChanged" PageSize="6" AllowPaging="true" OnSelectedIndexChanged="MasterGrid_SelectedIndexChanged" DataKeyField="Last Name">
                    <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
                    <SelectedItemStyle forecolor="White" backcolor="#9471DE"></SelectedItemStyle>
                    <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
                    <ItemStyle backcolor="#DEDFDE"></ItemStyle>
                    <Columns>
                        <asp:buttoncolumn text="Show details" commandname="Select" ItemStyle-Font-Bold="true" ItemStyle-Font-Size="smaller"></asp:buttoncolumn>
                    </Columns>
                </asp:datagrid>
                <br />
                <br />
                <asp:datagrid id="DetailsGrid" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" EnableViewState="False">
                    <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
                    <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
                    <ItemStyle backcolor="#DEDFDE"></ItemStyle>
                </asp:datagrid>
            </p>
        </form>
    </body>
    </html>
      

  3.   

    http://www.cnblogs.com/eddie005/archive/2006/07/04/442845.html
      

  4.   

    '页面代码
    <asp:GridView ID="GridView1" runat="server" RowStyle-Font-Size = "Medium" RowStyle-HorizontalAlign = "Center" ShowHeader = "False" 
                                AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" BorderColor = "Black" BorderStyle ="Solid" BorderWidth = "2" Width="488px" >
       <Columns >
          <asp:BoundField DataField ="custdo_id" HeaderText="id" />
       <asp:TemplateField>
         <ItemTemplate>
         <asp:Label ID="lblColumns2" Width = "120px" runat="server" Text='<%# bind("cust_name") %>' />
          </ItemTemplate>
         <ItemStyle BackColor="Transparent" BorderColor="Transparent" BorderStyle="Solid" BorderWidth="1px" />
          </asp:TemplateField>
             <asp:TemplateField>
        <ItemTemplate>
          <asp:GridView ID="GridView2" runat="server"  RowStyle-HorizontalAlign = "Center"
          AutoGenerateColumns="False" RowStyle-Font-Size = "Medium"  RowStyle-BackColor = "#ccccff" 
          ShowHeader = "False" OnRowDataBound = "GridView2_RowDataBound" CellPadding = "0" DataKeyNames="custitem_id" DataSourceID="SqlDataSource1">                     
              <RowStyle BackColor="#CCCCFF" Font-Size="Medium" HorizontalAlign="Center" />
              <Columns>
                  <asp:BoundField DataField="custitem_id" HeaderText="custitem_id" InsertVisible="False"
                      ReadOnly="True" SortExpression="custitem_id" />
                  <asp:BoundField DataField="custdo_id" HeaderText="custdo_id" SortExpression="custdo_id" />
                  <asp:BoundField DataField="item_type" HeaderText="item_type" SortExpression="item_type" />
                  <asp:BoundField DataField="item_name" HeaderText="item_name" SortExpression="item_name" />
                  <asp:BoundField DataField="item_size" HeaderText="item_size" SortExpression="item_size" />
                  <asp:BoundField DataField="item_xcpage" HeaderText="item_xcpage" SortExpression="item_xcpage" />
                  <asp:BoundField DataField="item_jzpage" HeaderText="item_jzpage" SortExpression="item_jzpage" />
                  <asp:BoundField DataField="item_tsyq" HeaderText="item_tsyq" SortExpression="item_tsyq" />
                  <asp:BoundField DataField="item_num" HeaderText="item_num" SortExpression="item_num" />
                  <asp:BoundField DataField="item_price" HeaderText="item_price" SortExpression="item_price" />
                  <asp:BoundField DataField="item_ok" HeaderText="item_ok" SortExpression="item_ok" />
                  <asp:BoundField DataField="cp" HeaderText="cp" SortExpression="cp" />
                  <asp:BoundField DataField="p_time" HeaderText="p_time" SortExpression="p_time" />
                  <asp:BoundField DataField="mem_gh" HeaderText="mem_gh" SortExpression="mem_gh" />
                  <asp:BoundField DataField="moyw" HeaderText="moyw" SortExpression="moyw" />
              </Columns>
       </asp:GridView> 
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:znhConnectionStringall %>"
                SelectCommand="SELECT * FROM [cust_item] where [custdo_id]=?">
                <SelectParameters >
                <asp:Parameter Name="custdo_id" Type="string" />
                </SelectParameters>
                </asp:SqlDataSource>
       
                  </ItemTemplate>                                   
        <ControlStyle BackColor="White" />
        <ItemStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
       </asp:TemplateField>
      </Columns>
       <RowStyle Font-Size="Medium" HorizontalAlign="Center" />
    </asp:GridView>
    '后台代码
     Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
          
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim s As SqlDataSource = CType(e.Row.FindControl("SqlDataSource2"), SqlDataSource)
                s.SelectParameters("custdo_id").DefaultValue = e.Row.Cells(0).Text
            End If    End Sub  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         
            Dim conn As String = System.Configuration.ConfigurationManager.ConnectionStrings("znhConnectionStringall").ToString
            Dim sql As String = "select custdo_id,cust_name from customer where cust_do_time='06-09-29'"
            Dim da As New SqlDataAdapter(sql, conn)
            Dim ds As New DataSet
            da.Fill(ds)
            GridView1.DataSource = ds.Tables(0)
            GridView1.DataBind()    End Sub