0

我刚开始使用ASP.NET GridView控件。

我正在考虑向中添加一个"Add New Row" Button以向.FooterDataRowGridView

最初,我希望网格是empty,只显示一个页脚行。但是,如果没有数据行,则整体GridView不会出现,也无法添加第一行,因为页脚也没有显示。

有没有办法只显示GridView一个页脚而没有数据行,还是我必须求助于一个 kludge?

4

4 回答 4

1

如果 ASP.NET DataGrid 没有任何行,则它不会显示任何内容(或者,如果您指定,也可以选择仅显示“无数据文本”值)。即使不存在数据或行,我们也希望至少显示网格标题。我们过去做过的一个技巧是在网格中添加一个空行。这将导致页眉/页脚出现。在标题的情况下,我们在空行上放置了一个 div,其中包含一些格式精美的文本……只是为了美化它。

于 2009-12-12T00:09:50.050 回答
1

您是否考虑过对 GridView 进行子类化并覆盖其 CreateChildControls 方法(如果需要,可能还有一些渲染方法)?

您也许可以更改其默认行为。如果这是可能的,它会比添加空行更简洁。

于 2009-12-12T00:20:44.820 回答
0

我不知道这是否可行。但是我们在 Telerik RadGrid 中遇到了类似的问题-> 如果 datasource = null,则网格不显示。出于某种原因,虽然它有一个“无记录模板”,但它不起作用。设置 datasource = new object[]{} 成功了,它显示了一个空网格。我的 2 美分

于 2009-12-12T01:29:18.437 回答
0

这里使用这个代码

  protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = "";
        if (!IsPostBack)
        {
            SetInitialRow();  
        }

    }
 private void SetInitialRow()
    {

        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));


        dr = dt.NewRow();
        dr["RowNumber"] = 1;

        dt.Rows.Add(dr);
        dt.TableName = "Coupons";
        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

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

    }

    private void AddNewRowToGrid()
    {

        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {

                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    drCurrentRow["Column1"] = box1.Text;

                    rowIndex++;

                }

                //add new row to DataTable
                dtCurrentTable.Rows.Add(drCurrentRow);
                //Store the current data to ViewState
                ViewState["CurrentTable"] = dtCurrentTable;

                //Rebind the Grid with the current data
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    private void SetPreviousData()
    {

        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");


                    box1.Text = dt.Rows[i]["Column1"].ToString();

                    rowIndex++;

                }
            }
        }
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }

和 aspx 代码

                                        <asp:GridView ID="Gridview1" runat="server" Style="text-align: center" ShowFooter="true" Width="70%    "
                                            AutoGenerateColumns="false">
                                            <Columns>
                                                <asp:BoundField DataField="RowNumber" HeaderText="Sr No" HeaderStyle-BackColor="#99CCCC" />
                                                <asp:TemplateField HeaderText="Coupon Code" HeaderStyle-BackColor="#99CCCC">
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="TextBox1" MaxLength="10" CssClass="form-control" runat="server"></asp:TextBox>
                                                    </ItemTemplate>


                                                    <FooterStyle HorizontalAlign="Right" />
                                                    <FooterTemplate>
                                                        <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CssClass="btn-toolbar btn" OnClick="ButtonAdd_Click" />
                                                    </FooterTemplate>
                                                </asp:TemplateField>
                                            </Columns>

                                            <%--    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />--%>
                                            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                                            <RowStyle BackColor="White" ForeColor="#003399" />
                                            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                            <SortedAscendingCellStyle BackColor="#EDF6F6" />
                                            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                                            <SortedDescendingCellStyle BackColor="#D6DFDF" />
                                            <SortedDescendingHeaderStyle BackColor="#002876" />
                                        </asp:GridView>

它只是一列,如果你想要我有一个带有删除功能的 4column 网格,你只需浏览代码并添加几列检查此链接以搜索我的评论 如何使用删除按钮删除网格视图中特定行的行?

或者你也可以使用这个 简单的插入在 ASP.Net GridView 控件中选择编辑更新和删除

于 2017-06-01T03:26:25.103 回答