0

I'm just starting to use the ASP.NET GridView control.

I was thinking of adding an "Add New Row" Button to the Footer to add a new DataRow to the GridView.

Initially, I want the grid to be empty, with only a footer row displayed. however, if there are no data rows, the whole GridView doesn't appear and there is no way to add first row since the footer also is not shown.

Is there a way to display a GridView with only a footer and no data rows or do I have to resort to a kludge?

0

4 Answers 4

1

Have you looked into subclassing the GridView and overriding its CreateChildControls method (and possibly some render methods as well if required)?

You may be able to alter its default behaviour. If this is possible, it would be less kludgy than adding empty rows.

Sign up to request clarification or add additional context in comments.

2 Comments

Good suggestion. But it must have been done before, preferablly in VB.NET, so I can learn and modify as needed.
If it's been done before, Google will surely have something. Failing that, you always have IntelliSense and Redgate Reflector if necessary.
1

The ASP.NET DataGrid will not show anything (or optionally show just a 'No Data Text value if you specify), if it does not have any rows. We wanted to show at least the grid header, even if no data or rows existed. One trick we have done in the past, is to add an empty row to the grid. This will cause the header / footer to appear. In the case of the header, we positioned a div over the empty row with some nicely formated text...just to pretty it up.

1 Comment

+1 Yeah, I was thinking of this, but I consider it a kludge. I'm surprised that the grid doesn't support it. Still looking...
0

I dont know if this would work. But we had a similar issue with Telerik RadGrid-> The grid does not display if datasource = null. For some reason though it had a "no records template" it did not work. setting datasource = new object[]{} did the trick, it showed an empty grid. my 2 cents

Comments

0

here use this code

  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();
    }

and for the aspx code

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

its just one column u go through the code and add few columns if u want I have a 4column grid with delete function in it check this link for that search for my comment How to delete a row using delete button for specific row in grid view?

or u can use this one too Simple Insert Select Edit Update and Delete in ASP.Net GridView control

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.