Monday 4 November 2013

How to bind empty GridView with header and custom message when no data present in Datatable in Asp.net

In this article i will explain how to bind GridView control with header i.e. with Column names and Custom message like "No Data Found" etc.It become necessary to bind empty gridview where there is no data in data source e.g Datatable.

Implementation: Let's create an asp.net application to see it in action.
  • Place a GridView control on the design file of asp.net website
        <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False">
                    <Columns>
<asp:BoundField DataField="EMP_NAME" HeaderText="Name" />
                         <asp:BoundField DataField="DEPT" HeaderText="Department" />
                         <asp:BoundField DataField="SALARY" HeaderText="Salary" />
                    </Columns>
         </asp:GridView>
  • Add following code in the code behind file
C#.Net Code to bind empty GridView with header and custom message

First include the following namespaces

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
then write code as:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }              
    }
     
private void BindGrid()
    {
        SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
        SqlDataAdapter adp=new SqlDataAdapter("select * from employee",con);
        DataTable dt=new DataTable();
        adp.Fill(dt);
        if (dt.Rows.Count>0 )
        {
            MyGridView.DataSource = dt;
            MyGridView.DataBind();
        }
        else
        {
            BingEmpyGridViewWithHeader(MyGridView, dt, "No data found");
        }
    }

    protected void BingEmpyGridViewWithHeader(GridView grd, DataTable dt,String msg)
{
            try
    {
                        if (dt.Rows.Count == 0) {
                                    //Add a blank row to the datatable
                                    dt.Rows.Add(dt.NewRow());
                                    //Bind the DataTable to the GridView
                                    grd.DataSource = dt;
                                    grd.DataBind();
                                    //Get the number of columns to know what the Column Span should be
                                    int columnCount = grd.Rows[0].Cells.Count;
                                    //Call the clear method to clear out any controls that you use in the columns.  E.g If you are using dropdown list etc. in any of the column then it is necessary.
                                    grd.Rows[0].Cells.Clear();
                                    grd.Rows[0].Cells.Add(new TableCell());
                                    grd.Rows[0].Cells[0].ColumnSpan = columnCount;
                                    grd.Rows[0].Cells[0].Text = "<font color=Red><b><center>"+ msg +"</center></b></font>";

                        }
            }
    catch (Exception ex)
    {
                        //Do your exception handling here
            }
}

No comments:

Post a Comment