Wednesday, 9 March 2016

How to add empty row into ASP.Net GridView with C#

In this article, we are going to learn inserting a empty row into gridview control when GridView doesn't have record.

In Designer File

In designer file, create a GridView control. Add three Label control in ItemTemplate for Id, EmpName, EmpAddress columns.

<div align="center">
    <asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="False"
        AllowPaging="true" ShowFooter="true" PageSize="5" BorderColor="SkyBlue">
        <HeaderStyle BackColor="#00A6DC" ForeColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="Employee Id">
                <ItemTemplate>
                    <asp:Label ID="lblId" runat="server" Text='<%#Eval("Id") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Name">
                <ItemTemplate>
                    <asp:Label ID="lblEmpName" runat="server" Text='<%#Eval("EmpName") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Address">
                <ItemTemplate>
                    <asp:Label ID="lblEmpAddress" runat="server" Text='<%#Eval("EmpAddress") %>'>
                    </asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
    </asp:GridView>
</div>

Namespaces Used

Include the below Namespace.

using System.Data;

Complete C# Code

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        BindGrid();
    }

}

private void BindGrid()
{
    DataSet ds = GetData();
    if (ds.Tables[0].Rows.Count > 0)
    {
        grdEmployee.DataSource = ds;
        grdEmployee.DataBind();
    }
    else
    {  //adding empty row
        ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
        grdEmployee.DataSource = ds;
        grdEmployee.DataBind();
        grdEmployee.Rows[0].Visible = false;
    }
}
//method returning zero record
private DataSet GetData()
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(Int32));
    dt.Columns.Add("EmpName", typeof(string));
    dt.Columns.Add("EmpAddress", typeof(string));
    ds.Tables.Add(dt);
    return ds;

}

Explanation

When page loads, BindGrid() method will get called which will add empty row to Gridview. GetData method will return zero record. Below is code to add blank row to DataSet.

                {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                grdEmployee.DataSource = ds;
                grdEmployee.DataBind();
                grdEmployee.Rows[0].Visible = false;
                } 

Output

Output will be shown as below.

add empty row into gridview
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON