Introdcution
What happens if your grid view control does not have any records to display? Well, if you set the EmptyDataText or EmptyDataTemplate properties, the grid will show something, only probably not what you'd like - the normal structure, as if it was populated with records. Here's a possible solution
Here I will explain how to display or show grid view header even if grid view does not contain data or show gridview header with no records.
Description
I got requirement like show the grid view header or header with message even if grid view does not contain any data. In our applications we have 3 example who show gird view
<%@ Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Display Empty grid with Header and messagetitle>
<style type="text/css">
.style1
{
width: 100%;
}
style>
<style type="text/css">
table.hovertable
{
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertable th
{
background-color: #c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertable tr
{
background-color: #d4e3e5;
}
table.hovertable td
{
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
style>
head>
<body>
<form id="form1"runat="server">
<div>
<table class="hovertable"align="center">
<tr>
<td>
Display grid with data
td>
tr>
<tr>
<td>
<asp:GridView ID="GridView"runat="server"AutoGenerateColumns="False"CellPadding="4"
ForeColor="#333333">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="recID"DataField="recID"/>
<asp:BoundField HeaderText="User Name" DataField="UserName" />
<asp:BoundField HeaderText="Email ID" DataField="EmailID" />
<asp:BoundField HeaderText="Gender"DataField="Gender"/>
<asp:BoundField HeaderText="DOB"DataField="DOB"/>
Columns>
asp:GridView>
td>
tr>
<tr>
<td>
Display Empty grid with Header and message
td>
tr>
<tr>
<td>
<asp:GridView ID="GridView1"runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="recID"DataField="recID"/>
<asp:BoundField HeaderText="User Name" DataField="UserName" />
<asp:BoundField HeaderText="Email ID" DataField="EmailID" />
<asp:BoundField HeaderText="Gender"DataField="Gender"/>
<asp:BoundField HeaderText="DOB"DataField="DOB"/>
Columns>
asp:GridView>
td>
tr>
<tr>
<td>
Display Empty grid with Header
td>
tr>
<tr>
<td>
<asp:GridView ID="GridView2"runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="recID"DataField="recID"/>
<asp:BoundField HeaderText="User Name" DataField="UserName" />
<asp:BoundField HeaderText="Email ID" DataField="EmailID" />
<asp:BoundField HeaderText="Gender"DataField="Gender"/>
<asp:BoundField HeaderText="DOB"DataField="DOB"/>
Columns>
asp:GridView>
td>
tr>
<tr>
<td>
td>
tr>
table>
div>
form>
body>
html>
using System;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingMySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
BindGridwithdata();
BindGridwithheaderMessage();
BindGridwithheader();
}
}
private void BindGridwithdata()
{
DataTable_dt = null;
try
{
stringconstring = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
MySqlConnection_con = new MySqlConnection(constring);
MySqlCommand_cmd = new MySqlCommand();
MySqlDataAdapter_da = new MySqlDataAdapter();
_dt = newDataTable();
if(_con.State == ConnectionState.Closed)
_con.Open();
_cmd.CommandText = "select * from userdetail";
_cmd.Connection = _con;
_da.SelectCommand = _cmd;
_da.Fill(_dt);
GridView.DataSource = _dt;
GridView.DataBind();
if(GridView.Rows.Count == 0)
{
EmptyGridFix(GridView);
//ShowNoResultFound(_dt, GridView);// call the Function}
}
}
catch (Exception ex)
{
ShowNoResultFound(_dt, GridView);
}
}
private void BindGridwithheaderMessage()
{
DataTable_dt = null;
try
{
stringconstring = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
MySqlConnection_con = new MySqlConnection(constring);
MySqlCommand_cmd = new MySqlCommand();
MySqlDataAdapter_da = new MySqlDataAdapter();
_dt = newDataTable();
if(_con.State == ConnectionState.Closed)
_con.Open();
_cmd.CommandText = "select * from userdetail_copy";
_cmd.Connection = _con;
_da.SelectCommand = _cmd;
_da.Fill(_dt);
GridView1.DataSource = _dt;
GridView1.DataBind();
if(GridView1.Rows.Count == 0)
{
ShowNoResultFound(_dt, GridView1);
}
}
catch (Exception ex)
{
ShowNoResultFound(_dt, GridView1);
}
}
private void BindGridwithheader()
{
DataTable_dt = null;
try
{
stringconstring = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
MySqlConnection_con = new MySqlConnection(constring);
MySqlCommand_cmd = new MySqlCommand();
MySqlDataAdapter_da = new MySqlDataAdapter();
_dt = newDataTable();
if(_con.State == ConnectionState.Closed)
_con.Open();
_cmd.CommandText = "select * from userdetail_copy";
_cmd.Connection = _con;
_da.SelectCommand = _cmd;
_da.Fill(_dt);
GridView2.DataSource = _dt;
GridView2.DataBind();
if(GridView2.Rows.Count == 0)
{
EmptyGridFix(GridView2);
}
}
catch (Exception ex)
{
EmptyGridFix(GridView2);
}
}
private void ShowNoResultFound(DataTablesource, GridView gv)
{
source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
// Bind the DataTable which contain a blank row to the GridView
gv.DataSource = source;
gv.DataBind();
// Get the total number of columns in the GridView to know what the Column Span should be
intcolumnsCount = gv.Columns.Count;
gv.Rows[0].Cells.Clear();// clear all the cells in the row
gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Black;
gv.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
gv.Rows[0].Cells[0].Text = "No Record Found.";
}
protected void EmptyGridFix(GridViewgrdView)
{
try
{
if(grdView.Rows.Count == 0 &&
grdView.DataSource != null)
{
DataTabledt = null;
if(grdView.DataSource is DataSet)
{
dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
}
elseif (grdView.DataSource isDataTable)
dt = ((DataTable)grdView.DataSource).Clone();
if(dt == null)
return;
dt.Rows.Add(dt.NewRow()); // add empty row
grdView.DataSource = dt;
grdView.DataBind();
// hide row
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
// normally executes at all postbacks
if(grdView.Rows.Count == 1 &&
grdView.DataSource == null)
{
boolbIsGridEmpty = true;
// check first row that all cells empty
for(int i = 0; i < grdView.Rows[0].Cells.Count; i++)
{
if(grdView.Rows[0].Cells[i].Text != string.Empty)
{
bIsGridEmpty = false;
}
}
// hide row
if(bIsGridEmpty)
{
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
}
}
catch (Exception ex)
{
}
}
}
Downloads
You can download the complete source code in C#
0 comments:
Post a Comment