【问题标题】:GridView Design : How to add a Blank New Row in GridView after button clickGridView 设计:单击按钮后如何在 GridView 中添加空白新行
【发布时间】:2014-03-18 06:25:58
【问题描述】:

我有 GridView,下面有一个 Button。单击按钮后,我想在 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);
        }
    }
}

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

【问题讨论】:

    标签: c# asp.net gridview data-binding


    【解决方案1】:

    我可以从您的问题中推断出您需要在 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();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      相关资源
      最近更新 更多