如何在不清除现有数据的情况下添加新网格行

时间:2013-08-10 15:45:20

标签: c# asp.net

我的代码在按钮单击时成功向gridview添加新行,但清除现有行中的所有输入值。据我所知,现有数据应存储在ViewState中,但我不知道在何处/如何实现这一点。

ASPX:

<asp:GridView ID="gvCommissions" runat="server" AutoGenerateColumns="False"
     CellPadding="4" ForeColor="#333333" AutoGenerateDeleteButton="True">                
  <Columns>
    <asp:TemplateField HeaderText="S.NO" ItemStyle-Width="5%">
     <ItemTemplate>
      <asp:Label ID="lblSno" runat="server" Width="98%"></asp:Label>
     </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="NAME" ItemStyle-Width="30%">
     <ItemTemplate>
      <asp:TextBox ID="txtName" runat="server" Width="98%"></asp:TextBox>
     </ItemTemplate>
  </Columns>
</asp:GridView>

背后的代码:

DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
       if(ViewState["gvCommissions"] != null) 
         dt = (DataTable)ViewState["gvCommissions"];
        if (!Page.IsPostBack)
        {
            GridViewStructure();
            AddNewRow();
        }
        else
        {
            dt = (DataTable)ViewState["gvCommissions"];
        }

        ViewState["gvCommissions"] = dt;  
    }

    private void GridViewStructure()
    {
        dt = new DataTable();
        dt.Columns.Add("sno");
        dt.Columns.Add("name");
    }            

    private void AddNewRow()
    {            
        dt = new DataTable();
        dt.Columns.Add("sno");
        dt.Columns.Add("name");

        foreach (GridViewRow gvRow in gvCommissions.Rows)
        {
            DataRow drCurrentRow = dt.NewRow();
            drCurrentRow["sno"] = ((Label)gvRow.FindControl("lblSno")).Text;
            drCurrentRow["name"] = ((TextBox)gvRow.FindControl("txtName")).Text;
            dt.Rows.Add(drCurrentRow);
        }

        DataRow dr = dt.NewRow();
        dr["sno"] = "";
        dr["name"] = "";

        dt.Rows.Add(dr);

        gvCommissions.DataSource = dt;
        gvCommissions.DataBind();

    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        AddNewRow();            
    }

2 个答案:

答案 0 :(得分:3)

简单首先检查gridview中是否有任何行,然后在datatable中添加行值,然后添加空行。如下所示。

protected void Page_Load(object sender, EventArgs e)
{      
    if (!Page.IsPostBack)
    {           
        AddNewRow();
    }        
}

private void AddNewRow()
{           
    DataTable dt = new DatatTable(); 
    dt.Columns.Add("sno");
    dt.Columns.Add("name");
    foreach (GridViewRow gvRow in gvCommissions.Rows)
    {
        DataRow dr = dt.NewRow();
        dr["sno"] = ((Label)gvRow.FindControl("lblSno")).Text;
        dr["name"] = ((Label)gvRow.FindControl("txtName")).Text;
        dt.Rows.Add(dr);
    }

    DataRow dr1 = dt.NewRow();
    dr1["sno"] = "";
    dr1["name"] = "";
    dt.Rows.Add(dr1);

    gvCommissions.DataSource = dt;
    gvCommissions.DataBind();

}


protected void btnAddRow_Click(object sender, EventArgs e)
{
    AddNewRow();            
}

答案 1 :(得分:1)

你错过了一部分。每次加载页面时,您都将表保存为查看状态,并忘记使用它!

尝试在页面加载时从viewstate检索它,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if(ViewState["gvCommissions"] != null) dt = (DataTable)ViewState["gvCommissions"];
    if (!Page.IsPostBack)
    {
        GridViewStructure();
        AddNewRow();
    }
    else
    {
        dt = (DataTable)ViewState["gvCommissions"];
    }

    ViewState["gvCommissions"] = dt;  
}

我要补充一点,将表保存在视图状态不是一个好主意。