Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net show gridview header when there is no data or empty

Dec 20, 2010
Introdcution

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.

Description

 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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="Name"/>
<asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
After that in codebehind write the following code 


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

Demo




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

Get the latest Asp.net, C#.net, 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

19 comments :

MLM Software said...

Nice Post!

Pooja

MLM Developers India

http://mlmdevelopers.com/products/mlm-software/corporate-mlm-soft/feature.html

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...

useful..

rajesh said...

you can use this also
ShowHeaderWhenEmpty="true"

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 asp.net 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 asp.net 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 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.