我页面中有一个DataGrid dg1
现在要求是在页面中动态添加两个DataGrid
样式和各种内容和这个dg1完全相同谢谢

解决方案 »

  1.   

    我是这样的
    我有DataGrid,然后在他绑定的时候让他加多一行来绑定其他的表如下是幕白兄的代码:
    DataGridItem item = e.Item;
            //GridViewRow NewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            DataGridItem NewItem = new DataGridItem(0, 0, ListItemType.Item);
            NewItem.Cells.Add(new TableCell());
            NewItem.Cells[0].ColumnSpan = item.Cells.Count;
            NewItem.Style.Add("display", "none");        dgMovie.Controls[0].Controls.Add(NewItem);
            DataGrid dg = new DataGrid();
            dg.DataSource = MovieOrder.GetList();
            dg.DataBind();
            NewItem.Cells[0].Controls.Add(dg);    
    现在可以实现我给DataGrid的行绑定多一个DataGrid的问题
    但是因为是new DataGrid我设计他里面的样式和绑定内容如果都用代码来写的话很是麻烦然后我就在外面自己设计了一个DataGrid然后代码为:
    DataGridItem item = e.Item;
            //GridViewRow NewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            DataGridItem NewItem = new DataGridItem(0, 0, ListItemType.Item);
            NewItem.Cells.Add(new TableCell());
            NewItem.Cells[0].ColumnSpan = item.Cells.Count;
            NewItem.Style.Add("display", "none");        dgMovie.Controls[0].Controls.Add(NewItem);
            DataGrid dg = mydg;   //这里是我自己设计的代码
            dg.DataSource = MovieOrder.GetList();
            dg.DataBind();
            NewItem.Cells[0].Controls.Add(dg);    
    本来以为这样是可行的,但是程序运行的时候有生成多一个行,但却没有生成我要的那个DataGrid谢谢,上面要怎么改
      

  2.   

    不会帮LZ UP  给点分
      

  3.   

    mydg 应该没有columns吧?
      

  4.   

    dgMovie.Controls[0].Controls.Add(NewItem);
            DataGrid dg = dg_Item;
            dg.DataSource = MovieOrder.GetList();
            dg.DataBind();
            NewItem.Cells[0].Controls.Add(dg);     
    还是不行.我给他加列
    绑定也成功了
    但是
    <tr class="BTableText" style="display:none;">
    <td align="center" colspan="6"></td>
    </tr>
    这是添加的行
    但是
        NewItem.Cells[0].Controls.Add(dg);     
    没有把他添加进来
    但是我用new datagird()就可以
      

  5.   

    dg_Item是写在页面上的吗?
      

  6.   

    就是你以前给我的那个添加行的
    因为用new datagrid()
    得出来的datagrid没有办法按我自己设计的样子
    要是写代码又太多
    所以想事先定一个
    然后就直接在
    DataGrid dg = dg_Item;
            dg.DataSource = MovieOrder.GetList();
            dg.DataBind();
            NewItem.Cells[0].Controls.Add(dg); 
    这样而不用DataGrid dg = new datagrid()
      

  7.   

    <asp:DataGrid ID="dg_Item" runat="server" AutoGenerateColumns="False" CssClass="BTableBorder" BorderWidth="1px" Width="100%" BackColor="WhiteSmoke" DataKeyField="MovieID">
      <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
      <ItemStyle CssClass="BTableText"></ItemStyle>
      <HeaderStyle Font-Bold="True" ForeColor="Black" CssClass="BTableHead" BackColor="LightBlue"  Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"></HeaderStyle>
                                        <Columns>                                                                      
      <asp:BoundColumn DataField="AgentPrice" HeaderText="代理价">
      <HeaderStyle HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
      <ItemStyle HorizontalAlign="Center" />
      </asp:BoundColumn>  
      <asp:BoundColumn DataField="SuggestPrice" HeaderText="建议价">
      <HeaderStyle HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
      <ItemStyle HorizontalAlign="Center" />
      </asp:BoundColumn>  
      <asp:BoundColumn DataField="WeekAgentPrice" HeaderText="周末代理价">
      <HeaderStyle HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
      <ItemStyle HorizontalAlign="Center" />
      </asp:BoundColumn>  
      <asp:BoundColumn DataField="WeekSuggestPrice" HeaderText="周末建议价">
      <HeaderStyle HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
      <ItemStyle HorizontalAlign="Center" />
      </asp:BoundColumn>  
      <asp:BoundColumn DataField="MovieTypeID" HeaderText="票类型">
      <HeaderStyle HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
      <ItemStyle HorizontalAlign="Center" />
    </asp:BoundColumn>   
    </Columns>
    </asp:DataGrid>
    后台绑定
     protected void dgMovie_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem item = e.Item;
            //GridViewRow NewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            DataGridItem NewItem = new DataGridItem(0, 0, ListItemType.Item);
            NewItem.Cells.Add(new TableCell());
            NewItem.Cells[0].ColumnSpan = item.Cells.Count;
            NewItem.Style.Add("display", "none");        dgMovie.Controls[0].Controls.Add(NewItem);
            DataGrid dg =dg_Item;
            //DataGrid dg = new DataGrid();
            dg.DataSource = MovieOrder.GetList();
            dg.DataBind();
            NewItem.Cells[0].Controls.Add(dg);        
        }
      

  8.   

    NewItem.Style.Add("display", "none");
    ??这行应该去掉
      

  9.   

    去掉了也不行
    <tr class="BTableText">
    <td align="center" colspan="6"></td>
    </tr>