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. |
|||
|
|||
19 comments :
Nice Post!
Pooja
MLM Developers India
http://mlmdevelopers.com/products/mlm-software/corporate-mlm-soft/feature.html
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.
Works!! Thank u :)
useful..
you can use this also
ShowHeaderWhenEmpty="true"
Column 'ID' does not allow nulls. error,
can u please help me
Hope you got the problem fixed!
The header still can not show, I don't know why!
No....Its not Working
this is very useful post. thank you
how to do it when gridview is attached to sqldatasource
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.
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.............
hi sir,
can u plz help how to create MLM DATA ENTRY web application asp.net 4.0 c# with sql code
Error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
great! thanks
Hi suresh, Gridview empty message have problem on dropdownlist autopostback=true
Error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Note: Only a member of this blog may post a comment.