Show Grid Header's if data is not present


In this article I am trying to explain How to show GridView Headers without Data. If we try to bind GridView without data then GridView controls has not been shown in Web Page for this I wrote one simple method to display headers if data is not present.

Show Grid Header's if data is not present:



In this article I'm trying to explain How to display GridView control if data is not present in Table.

Description:


In this article I'm trying to explain How to show GridView Headers without Data. If we try to bind GridView without data then GridView controls has not been shown in Web Page. But that's not I'm expecting If Data is present or not Present I want to Visible GridView control Headers, for this I wrote one simple method to show empty GridView With Headers.

Source Code:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridHeaders.aspx.cs" Inherits="GridHeaders" %>

<!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></title>
<style type="text/css">
.style1
{
font-size: large;
color: #003300;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<strong><span class="style1">GridView Headers without Data</span></strong><br />
<br />
<asp:GridView ID="GV" runat="server" AutoGenerateColumns="False"
CellPadding="3" ForeColor="Black" GridLines="Vertical" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" >
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="Employee Number">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"EmpID") %>' ></asp:Label>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Ename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job">
<ItemTemplate>
<asp:Label ID="lblJob" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Job") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:Label ID="lblResult" runat="server" Text="There is no data to bound"
Visible="False" style="color: #CC0000"></asp:Label>

</div>
</form>
</body>
</html>


Code Behind:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class GridHeaders : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpID");
dt.Columns.Add("Ename");
dt.Columns.Add("Job");

if (dt.Rows.Count > 0)
{
GV.DataSource = dt;
GV.DataBind();
lblResult.Visible = false;
}
else
{
DataRow dr = dt.NewRow();

dt.Rows.Add(dr);
dt.AcceptChanges();
GV.DataSource = dt;
GV.DataBind();
GV.Rows[0].Visible = false;
lblResult.Visible = true;
}
}

}


Output:



GridHeaders

Conclusion:


This article will be helpful to you while Show Grid Headers if data is not present.


Attachments

  • Source Code (45187-22346-Source-Code.zip)
  • Article by naveensanagasetti
    I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

    Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: