Show Gridview When there is no data.?

Naveenhcl
Posted by Naveenhcl under ASP.NET category on | Points: 40 | Views : 359
In this code part I just explain how to display the GridView, when there is no data. Refer below sample code for display the Gridview headers if there is no data.

private void ShowNoRecordsFound(DataTable dt, GridView gv)
{
dt.Rows.Add(dt.NewRow());
gv.DataSource = dt;
gv.DataBind();

int colcount = gv.Columns.Count;
gv.Rows[0].Cells.Clear();
gv.Rows[0].Cells.Add(new TableCell());
gv.Rows[0].Cells[0].ColumnSpan = colcount;
gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Teal;

gv.Rows[0].Cells[0].Text = "NO RECORDS FOUND";
gv.Rows[0].Attributes["onclick"] = "";
}


Call above method while binding GridView.

protected void Bind_Grid()
{
DataSet ds=//data;
if(ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)
{
gv.DataSource=ds;
gv.DataBind();
}
else
{
ShowNoRecordsFound(ds.Tables[0],gv);
}
}


Hope this will help you those who are looking for the same..

Comments or Responses

Login to post response