繁体   English   中英

GridView设计:如何在单击按钮后在GridView中添加空白新行

[英]GridView Design : How to add a Blank New Row in GridView after button click

我有下面的一个按钮的GridView。 单击按钮后,我想在GridView下方添加一个新的空白行

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnTest" Text="Test" runat="server" OnClick="btnTest_Click" />
        </div>

        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <strong>Dynamic Grid</strong></td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="true"
                         OnRowDataBound="GrdDynamic_RowDataBound" >
                    </asp:GridView>
  </td>
            </tr>
        </table>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </form>
</body>
</html>

并通过以下方式填充数据...

在pageLoad中,我称绑定

 DataTable dt = GroupBudgetSetUpBLL.Instance.GetAllGroupBudgetSetUp();
            GrdDynamic.DataSource = dt;
            GrdDynamic.DataBind();

在rowdatabound中,我正在制作Grid Editable文本框。

protected void GrdDynamic_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 1; i < e.Row.Cells.Count; i++)
        {
            TextBox txt = new TextBox();
            string budget = e.Row.Cells[i].Text.Split('_').LastOrDefault();
            string txtID = e.Row.Cells[i].Text.Split('_').FirstOrDefault() + "_" + e.Row.Cells[i].Text.Split('_').Skip(1).FirstOrDefault();
            txt.ID = txtID;
            txt.Text = budget;
            e.Row.Cells[i].Text = "";
            e.Row.Cells[i].Controls.Add(txt);
        }
    }
}

现在,单击网格下方的“按钮”后,我想在MainGrid下方显示空白行,单元格控件类型将为TextBox,以便保持可编辑状态。

我可以从您的问题中推断出,您需要在GridView下面添加一个新的空白行(看起来GridView的最后一行在button_click上为空白)。

我不确定将新行添加到数据绑定的网格视图是否可行,但是您可以做的是在数据表中添加空白行,如下所示:

将以下代码添加到您的button1_Click()

protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt = GroupBudgetSetUpBLL.Instance.GetAllGroupBudgetSetUp();
            DataRow myRow = dt.NewRow();
            dt.Rows.InsertAt(myRow, dt.Rows.Count);
            GrdDynamic.DataSource = dt;
            GrdDynamic.DataBind();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM