aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies show gridview header when there is no data or empty

Dec 20, 2010

Here I will explain how to display or show gridview header even if gridview does not contain data or show gridview header with no records.


 In one application I got requirement like show the gridview header even if grddview does not contain any data. In our applications we will send data to dataset and bind that dataset to gridview to display data on form. In gridview we have property called EmptyDataText by using this property we will display message like "No records Found" if gridview contains empty data. It will display the message but it won’t show header of gridview or anything simply it will display only that message (whatever the message we gave to EmptyDataText property). 

It will display message like this without gridview headers

In some situations we need to display Gridview header with No Records Found message at that time what we can do?
To solve that problem we need to write the some functionality in code behind 

Design aspx page like this

<html xmlns="">
<head id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
<form id="form1" runat="server">
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<asp:BoundField DataField="UserName" HeaderText="Name"/>
<asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
After that in codebehind write the following code 

protected void Page_Load(object sender, EventArgs e)
protected void BindGridviewData()
SqlConnection con = new SqlConnection("Data Source=MYSystem;Initial Catalog=MySamplesDB;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select UserName,FirstName,LastName,Location from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
gvdata.DataSource = ds;
int columncount = gvdata.Rows[0].Cells.Count;
gvdata.Rows[0].Cells.Add(new TableCell());
gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata.Rows[0].Cells[0].Text = "No Records Found";
gvdata.DataSource = ds;


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


MLM Software said...

Nice Post!


MLM Developers India

MLM software said...

Hi! I am a web based business man. And as we all know, Web video is an advantageous tool for marketing, advertising, product information, presentations, training or promoting key messages. This site is offering a comprehensive video on the web service. And this site is providing with the best created web videos. These guys are really great.

sneha said...

Works!! Thank u :)

Anonymous said...


rajesh said...

you can use this also

Rakesh said...

Column 'ID' does not allow nulls. error,
can u please help me

hermes said...

Hope you got the problem fixed!

Rakesh said...
This comment has been removed by the author.
home said...

The header still can not show, I don't know why!

Unknown said...

No....Its not Working

Uttara said...

this is very useful post. thank you

Anonymous said...

how to do it when gridview is attached to sqldatasource

Unknown said...

dear sir,
i am using vs 2010 c#.
how to auto generate id after data save in sql

but now i generate id like IME000001 its ok for me.
but i don't want show id to any user before he register in my web site.
i want to when client go to register page he field all details, than he click on save button and display id WITH ALL DATA like IME000001.

Anonymous said...

Hi sir,
can u plz help me in solving this problem,
i want a empty table that should be in the form of grid view,as soon as user enters the data it should automatically got save in db..............please rpy soon.............

Unknown said...

hi sir,
can u plz help how to create MLM DATA ENTRY web application 4.0 c# with sql code

Anonymous said...

Error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Anonymous said...

great! thanks

Unknown said...

Hi suresh, Gridview empty message have problem on dropdownlist autopostback=true

Anonymous said...

Error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.