using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace NAMe
{
public partial class Details : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
//Show Empty Data Grid With Header and Footer When DataTable or Dataset is NULL
this.ShowEmptyGrid(grdView);
if (!Page.IsPostBack)
{
}
}
}
protected void poulategrid(string val)
{
DataTable dt =
grdView.DataSource =
grdView.DataBind();
//Show Empty Data Grid With Header and Footer When DataTable or Dataset is NULL
this.ShowEmptyGrid(grdView);
}
//Show Empty Data Grid With Header and Footer When DataTable or Dataset is NULL
protected void ShowEmptyGrid(GridView grdView)
{
// Executes After Grid Load Method
if (grdView.Rows.Count == 0 &&
grdView.DataSource != null)
{
DataTable dt = null;
//We have To clone the Sources
if (grdView.DataSource is DataSet)
{
dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
}
else if (grdView.DataSource is DataTable)
{
dt = ((DataTable)grdView.DataSource).Clone();
}
if (dt == null)
{
return;
}
//Adding Empty Row
dt.Rows.Add(dt.NewRow());
grdView.DataSource = dt;
grdView.DataBind();
//Hiding The Row
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
}
}
}
Upamanyu Roy ChoudhuryPosted Feb 23, 2013, 1:18 AM
I do not think there is any need to add blank or empty rows to the datagridview.
Say we are fetching data from DB and storing in sat datatable dt.
Now say place your datagridview inside a panel and bind the datasource to dt.
Here if dt is null the gridview will be empty in its own , to make it sure just set the visibility of the panel to false and show say another panel say showing a text
" No Data To Display "
dt = GetdataFromDB();
DataGridView1.DataSource = dt;
DataGridView1.DataBind();
if(dt!=null)
{
// go ahead with whatever you want to do
}
else
{
PanelGrid.Visible=false; // panel that holds the DataGridView
PanelError.Visible=true;// panel that shows the message
}
Hope it helps,keep me posted