自定义GidView内部GridView Asp.net c#

本文关键字:net Asp GridView GidView 内部 自定义 | 更新日期: 2023-09-27 18:20:08

我正在开发一个多级网格视图,仅用于获取输入。

核心概念是在父网格中,我有一个用于项目名称的模板字段,而在另一个模板字段中我有子网格视图。其具有用于工作描述的模板字段。

功能设置方式为

我成功地在父网格中添加了新行,但不知道如何在子网格中插入新行。我的代码是

aspx代码是

   <asp:GridView  OnRowDataBound="grid1_RowDataBound" AutoGenerateColumns="false" ShowFooter="true" runat="server" ID="grid1">
     <Columns>
         <asp:TemplateField HeaderText="sn#">
           <ItemTemplate>
              <%# Container.DataItemIndex + 1 %>
           </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Project">
           <ItemTemplate>
               <asp:TextBox runat="server" ID="txtProject" ></asp:TextBox>
           </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
           <ItemTemplate>
               <asp:GridView runat="server" ID="grid2" AutoGenerateColumns="false" ShowFooter="true">
                 <Columns>
         <asp:TemplateField>
            <ItemTemplate>
              <%# Container.DataItemIndex +1 %>
            </ItemTemplate>
         </asp:TemplateField>                      
         <asp:TemplateField HeaderText="WorkDone">
             <ItemTemplate>
                <asp:TextBox runat="server" ID="txtWorkDescription"></asp:TextBox>
             </ItemTemplate>
             <FooterTemplate>
             <asp:Button ID="btnAddNewRowGrid2" runat="server" Text="Add new row" OnClick="btnAddNewRowGrid2_Click" />
             </FooterTemplate>
         </asp:TemplateField>
       </Columns>
  </asp:GridView>
  </ItemTemplate>
  <FooterTemplate>
  <asp:Button ID="btnAddNewRowGrid1" runat="server" Text="Add new row" OnClick="btnAddNewRowGrid1_Click" />
  </FooterTemplate>
  </asp:TemplateField>
  <asp:CommandField ShowDeleteButton="True" />
  </Columns>
 </asp:GridView>

I implemented the code in this way:

 private void LoadFirstGrid1Row()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("ProjectId", typeof(string)));
        dt.Columns.Add(new DataColumn("WorkList", typeof(List<string>)));
        dt.Columns.Add(new DataColumn("Grids", typeof(GridView)));
        dr = dt.NewRow();
        dr["ProjectId"] = string.Empty;
        dr["WorkList"] = null;
        dr["Grids"] = null;
        dt.Rows.Add(dr);
        ViewState["Grid1"] = dt;
        grid1.DataSource = dt;
        grid1.DataBind();
    }

    private void AddNewRowGrid1()
    {
        int rowIndex = 0;
        if(ViewState["Grid1"] !=null)
        {
            DataTable dt = (DataTable)ViewState["Grid1"];
            DataRow dr = null;
            if(dt.Rows.Count >0)
            {
                for(int i = 1;i<=dt.Rows.Count;i++)
                {
                    TextBox txtproject = (TextBox)grid1.Rows[rowIndex].Cells[1].FindControl("txtProject");
                    GridView grid2 = (GridView)grid1.Rows[rowIndex].Cells[2].FindControl("grid2");
                    var lst = new List<string>();
                    foreach(GridViewRow row in grid2.Rows)
                    {
                        TextBox txtWorkDescription = (TextBox)grid2.Rows[row.RowIndex].FindControl("txtWorkDescription");
                        lst.Add(txtWorkDescription.Text);
                    }
                    dr = dt.NewRow();
                    dt.Rows[i - 1]["ProjectId"] = txtproject.Text;
                    dt.Rows[i - 1]["WorkList"] = lst;
                    rowIndex++;
                }
                dt.Rows.Add(dr);
                ViewState["Grid1"] = dt;
                grid1.DataSource = dt;
                grid1.DataBind();
            }
        }
        SetGrid1PreviousData();
    }
    private void SetGrid1PreviousData()
    {
        int rowIndex = 0;
        if (ViewState["Grid1"] != null)
        {
            DataTable dt = (DataTable)ViewState["Grid1"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtProject = (TextBox)grid1.Rows[rowIndex].Cells[1].FindControl("txtProject");
                    GridView grid2 = (GridView)grid1.Rows[rowIndex].Cells[2].FindControl("grid2");
                    var lst = new List<string>();
                    lst = null;
                    try
                    {
                        lst = (List<string>)dt.Rows[i]["WorkList"];
                        grid2.DataSource = lst;
                        grid2.DataBind();
                    }
                    catch {
                        LoadFirstGrid2Row(grid2);                            
                    }                   
                    txtProject.Text = dt.Rows[i]["ProjectId"].ToString();                        
                    rowIndex++;
                }
            }
        }
    }

    protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView grid2 = (GridView)e.Row.FindControl("grid2");
            LoadFirstGrid2Row(grid2);
        }
    }
    protected void btnAddNewRowGrid1_Click(object sender, EventArgs e)
    {
        AddNewRowGrid1();
    }
    #endregion
    #region Grid2
    private void LoadFirstGrid2Row( GridView grid2)
    {
        DataTable dt = new DataTable();
        DataRow dr = null;                       
        dt.Columns.Add(new DataColumn("WorkDescription", typeof(List<string>)));            
        dr = dt.NewRow();            
        dr["WorkDescription"] = null;          
        dt.Rows.Add(dr);
        ViewState["Grid2"] = dt;  
        grid2.DataSource = dt;
        grid2.DataBind();
    } 
    protected void btnAddNewRowGrid2_Click(object sender, EventArgs e)
    {
    }

任何正文建议plz我可以在内部网格视图中添加新行

自定义GidView内部GridView Asp.net c#

由于您已经定义了页脚模板,因此

protected void btnAddNewRowGrid2_Click(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
string workdonetxt= (row.FindControl("txtWorkDescription") as TextBox).Text;
}

调用insert或add函数并传递值。

在网格视图中添加行的示例。