Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example demonstrates how to show the Header and Footer of GridView when no data returned from the DataSet or DataTable. 

The trick here is to add a new blank row to the DataTable that you used as your DataSource if there was no data returned in your query.

Here’s the method for showing the Header and Footer of GridView when no data returned in the query.

    private void ShowNoResultFound(DataTable source, 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
        int columnsCount = 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.Red;
        gv.Rows[0].Cells[0].Font.Bold = true;
        //set No Results found to the new added cell
        gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
    }

 

As you can see, the method above takes two paramaters, first is the DataTable that you used as the DataSource, second is the ID of your GridView.

All you need to do is Call the method ShowNoResultFound() when your DataSource ( the DataTable) returns nothing. See this example below:

private void BindGridView()
{ 
        DataTable dt = new DataTable(string user);
        SqlConnection connection = new SqlConnection(GetConnectionString()); 
        try
        {
            connection.Open();
            string sqlStatement = "SELECT* FROM Orders WHERE UserID = @User";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
            sqlCmd.Parameters.AddWithValue("@User", user);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);
            if (dt.Rows.Count > 0) //Check if DataTable returns data
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            Else //else if no data returned from the DataTable then 
            {    //call the method ShowNoResultFound()
                ShowNoResultFound(dt,GridView1);
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
}

 

Here’s the page out below:

I hope someone find this post useful!

Posted on Wednesday, March 11, 2009 4:10 PM ADO.NET , ASP.NET , C# , Tips&Tricks , GridView | Back to top


Comments on this post: Tip/Trick: Show Header and Footer of GridView when no Data returned.

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi,

Nice post Vincent!

Would you be interested in writing a detailed about about this scenario and posting it on www.highoncoding.com
Left by Mohammad Azam on Mar 12, 2009 1:26 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
It's very useful
Left by Radhika on Mar 17, 2009 8:08 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
thnks a lot
Left by suman on Mar 26, 2009 7:15 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Great Post, Thanks!

If you are editing data and don't want to deal with a bogus empty row in your table, you can do this:

DataTable t = source.Clone(); // Clone Source Table
foreach (DataColumn c in t.Columns)
c.AllowDBNull = true; // Allow Nulls in all columns
t.Rows.Add(t.NewRow()); // Add empty row

gv.DataSource = t; // Set Source to clone table with 1 row
gv.DataBind(); // bind to empty table
.
.
.
gv.Rows[0].Cells[0].Text = gv.EmptyDataText; // Use GridView's Empty Row message
Left by Randy on Apr 05, 2009 7:25 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Great Post Indeed..Thanks you made my day

Left by Rave on Apr 15, 2009 8:20 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
if i have a 3 clo. and disabled auto generate columns in my gridview i got this error
could u plz advice me how can i solve it .
my first column is ID

:: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.

Line 58: gv.DataBind(); // bind to empty table
Line 59: // Get the total number of columns in the GridView to know what the Column Span should be
Left by Waleed Mohamed on Apr 22, 2009 10:46 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
good article.
Left by pravesh on May 19, 2009 8:16 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi,
Great Post!! But I am getting this given error. Could anyone resolve this problem.

System.Data.NoNullAllowedException: Column 'SubjectID' does not allow nulls.
Left by kumar on Jun 03, 2009 4:42 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Thanks Randy!!

You made my job easy!!


I slightly added few lines of code!!

private void BindEmptyRow (DataTable source, GridView gv)
{
DataTable t = source.Clone(); // Clone Source Table

foreach ( DataColumn c in t.Columns )
c.AllowDBNull = true; // Allow Nulls in all columns

t.Rows.Add( t.NewRow() ); // Add empty row

gv.DataSource = t; // Set Source to clone table with 1 row
gv.DataBind(); // bind to empty table
//gv.Rows[0].Cells[0].Text = gv.EmptyDataText; //If you dont want to show the Message

gv.Rows[0].Visible = false;
gv.Rows[0].Controls.Clear();
}
Left by kumar on Jun 03, 2009 4:51 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Very good article.. doesnt need to overwrite or create any custom control unlike the rest. Efficient!!
Left by ckk on Jun 26, 2009 4:11 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
hi,
Nice post
Left by kesava on Sep 09, 2009 6:07 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Nice Posting, It worked for me. Thanks!!
Left by Gowri on Sep 23, 2009 7:39 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
please check this post also i think it will be useful if you use entity framework
http://ledomoon.blogspot.com/2009/04/show-grid-view-header-and-footer-when.html
Left by Waleed Mohamed on Oct 08, 2009 11:04 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Thankx It is working.
Left by Pradeep Manker on Oct 30, 2009 7:03 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi, Thanks for the Perfect code. It just works great. Thanks once again.
Left by Pankul on Dec 14, 2009 1:04 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
please i want to enter my data into data table which one i create in to asp .net

datatable dt = new datatable();

i want insert my data into this table from text box
Left by mandip on Feb 20, 2010 4:37 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi,
Its very usefull.I am just looking for that.
Thanks
Left by Raj on Mar 24, 2010 8:42 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
hi. I'm really having an issue. It's not exactly related but still on the Gridview.
I have a gridview

1. I want to insert an image into the footer of the last field
2. When users click this image, I want to export the data to excel. (I have the code to export to excel and it works fine)

My GRID is in an updatepanel. can anyone help please?
Left by don on Mar 26, 2010 5:03 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
WONDERFUL!!!!! The post from Randy to clear the DBNull made it a little better!!!!!
Left by MTRIG on Nov 17, 2010 8:50 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi guys this is very easy and understansble to work..... Thanks
Left by sildhas on Feb 03, 2011 2:23 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi,

I am getting a runtime error at gv.Rows[0].Cells.Clear();
the error states that: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index. any idea of how to resolve this
Left by Chaitanya on Feb 05, 2011 6:38 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Simple and optimal solution, keep up the good work
Left by Mani on Mar 04, 2011 1:26 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
This is very helpful! Thanks!
Left by Vo on Mar 16, 2011 10:25 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Outstanding !!! so nice & simple...gr8888
Left by Mehboob Khan Yousafzai on Mar 17, 2011 2:17 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
nice post. I have used the same.
Left by neeleshgour on Mar 29, 2011 2:46 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
this is very easy thanks a lot
Left by sarfaraz khan on Jun 04, 2011 7:58 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Hi,

When in the footer row we have an ajaxToolkit object like CascadingDropDown, if our gridview has no results and call the ShowNoResultFound function, It shows The footer row without the ajaxToolkit object.

If we have records in our gridview the footer row with the ajaxToolkit object working perfectly.

Any help?

Thank you very much
Left by jaume on Jun 21, 2011 11:48 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
My last comment was solved, is another error... :p
Left by jaume on Jun 22, 2011 12:16 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Thank's Realy nice and useful article
Left by Arun Patidar on Sep 12, 2011 6:38 PM

# Prb with empty girdview row
Requesting Gravatar...
First row of my gridview contains heading for columns and 2nd row is empty.
While inserting record in datbase on click of submit button, m getting exception that input string is not in correct format. My actual data starts from 3rd row. How to delete this empty row and fetch data from the gridview
Left by Rash on Sep 27, 2011 3:14 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Thanks for help me
Left by Kent on Oct 04, 2011 1:51 AM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Thank you so much it works great!!!
Left by Sasikumar on Oct 31, 2011 1:57 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
It's Great!!! Saw different solutions but It's the best wihtout changing a lot.
Left by Brij on Dec 02, 2011 8:24 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Great post! I've tried and it works well. However, I have another solution. Simply using NewRow() method of DataTable to add a new blank row. Take a look at this: http://programmingshare-thienle.blogspot.com/2012/01/show-header-and-footer-of-gridview-with.html
Left by Thien Le on Jan 03, 2012 2:31 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
so nicely explained... thank u....
Left by ranjan on Feb 06, 2012 3:22 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Very Good Post And Running Very good.
on My code thanks for shareing
Left by ChanChal Singh on May 09, 2012 1:24 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...

I Used Drop down list filtering for Gridview, but this error occurs:
Fetch Error:The parameterized query '(@Value1 nvarchar(4000),@Value2 nvarchar(16),@Value3 int)select ' expects the parameter '@Value1', which was not supplied.
please help me.
Left by somi on Jun 18, 2012 11:50 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
A very useful article ..thanks alot
Left by Kumail on Aug 08, 2012 12:56 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
ITS FOR YOU SOMI....

This problem occurs when u don't bind grid view at the right place ...bind your grid view at the insert button or ADD NEW RECORD button....MAY be it will help u ...
Left by Kumail on Aug 08, 2012 1:00 PM

# re: Tip/Trick: Show Header and Footer of GridView when no Data returned.
Requesting Gravatar...
Used this approach in one of my projects. Thanks for sharing!
Left by Priya on Feb 17, 2013 12:30 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net