Wednesday, May 12, 2010

Displaying Grid View With empty Data Source.

Step1: Check your data source.
Step2: if in data source there is no record then send grid view control with in this method and change the name of grid view wherever it is necessary.

private void BindGridView(GridView GrdQuali)
{
//Check to ensure we have zero rows in our GridView
//and that our GridView has been assigned a DataSource
if (GrdQuali.Rows.Count == 0 && GrdQuali.DataSource != null)
{
//Create ourselves a DataTable to
//hold our "dummy" row
DataTable dt = null;
// need to clone sources otherwise it will be indirectly adding to
// the original source
//Check to see if the DataSource of our grid is a DataTable or DataSet
if (GrdQuali.DataSource is DataSet)
{
dt = ((DataSet)GrdQuali.DataSource).Tables[0].Clone();
}
else if (GrdQuali.DataSource is DataTable)
{
dt = ((DataTable)GrdQuali.DataSource).Clone();
}
//Check to ensure our DataTable object was assigned a value
if (dt == null)
{
return;
}
// now we add our empty row
dt.Rows.Add(dt.NewRow());
//Bind our GridView
GrdQuali.DataSource = dt;
GrdQuali.DataBind();
//hide our "dummy" row
GrdQuali.Rows[0].Visible = false;
GrdQuali.Rows[0].Controls.Clear();
}
}

1 comment: