One thing thats midly annoying to me about the new gridview control is that it doesn't display the header row when there is no data. I would like to just have the header displayed like the original table with a single row saying something like "No records found". The grid has a property for emptydata text which doesn't look that great in my opinion. I have seen some people use the <> property to create a new table in there which isn't the cleanest solution. One way to get the gridview to display how you want is to create a custom control that inherts from gidview. Then you can override the CreateChildControls method to create a the gridview with the all the headers and a blank row with one cell and a text message. Here's how to do it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
public class EmptyGridView : GridView
{
#region Properties
///
/// Enable or Disable generating an empty table if no data rows in source
///
[Description("Enable or disable generating an empty table with headers if no data rows in source"),
Category("Misc"),
DefaultValue("true"),]
public bool ShowEmptyTable
{
get
{
object o = ViewState["ShowEmptyTable"];
return (o != null ? (bool)o : true);
}
set
{
ViewState["ShowEmptyTable"] = value;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
public class EmptyGridView : GridView
{
#region Properties
///
/// Enable or Disable generating an empty table if no data rows in source
///
[Description("Enable or disable generating an empty table with headers if no data rows in source"),
Category("Misc"),
DefaultValue("true"),]
public bool ShowEmptyTable
{
get
{
object o = ViewState["ShowEmptyTable"];
return (o != null ? (bool)o : true);
}
set
{
ViewState["ShowEmptyTable"] = value;
}
}
///
/// Get or Set Text to display in empty data row
///
[Description("Text to display in empty data row"),
Category("Misc"),
DefaultValue("),]
public string EmptyTableRowText
{
get
{
object o = ViewState["EmptyTableRowText"];
return (o != null ? o.ToString() : ");
}
set
{
ViewState["EmptyTableRowText"] = value;
}
}
#endregion
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
int numRows = base.CreateChildControls(dataSource, dataBinding);
//no data rows created, create empty table if enabled
if (numRows == 0 && ShowEmptyTable)
{
//create table
Table table = new Table();
table.ID = this.ID;
//create a new हेडर row
GridViewRow row
= base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
//convert the exisiting columns into an array and initialize
DataControlField[] fields = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(fields, 0);
this.InitializeRow(row, fields);
table.Rows.Add(row);
//create the empty row
row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = this.Columns.Count;
cell.Width = Unit.Percentage(100);
cell.Controls.Add(new LiteralControl(EmptyTableRowText));
row.Cells.Add(cell);
table.Rows.Add(row);
this.Controls.Add(table);
}
return numRows;
}
}
//convert the exisiting columns into an array and initialize
DataControlField[] fields = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(fields, 0);
this.InitializeRow(row, fields);
table.Rows.Add(row);
//create the empty row
row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = this.Columns.Count;
cell.Width = Unit.Percentage(100);
cell.Controls.Add(new LiteralControl(EmptyTableRowText));
row.Cells.Add(cell);
table.Rows.Add(row);
this.Controls.Add(table);
}
return numRows;
}
}
Naimish Dave
Comments