簡體   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