Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 124, comments - 366, trackbacks - 0

My Links

News

Archives

Image Galleries

Tip/Trick: Show Header and Footer of GridView when no Data returned.

This example demonstrates on 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:


Print | posted on Wednesday, March 11, 2009 4:10 PM |

Feedback

Gravatar

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

Hi,

Nice post Vincent!

Would you be interested in writing a detailed about about this scenario and posting it on www.highoncoding.com
3/12/2009 1:26 AM | Mohammad Azam
Gravatar

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

Hi Azam,

Sure, I will just inform you when I'm done writing a detailed information about it..

Thanks.
3/12/2009 1:15 PM | Vinz
Gravatar

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

It's very useful
3/17/2009 8:08 PM | Radhika
Gravatar

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

thnks a lot
3/26/2009 7:15 PM | suman
Gravatar

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

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
4/5/2009 7:25 AM | Randy
Gravatar

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

Great Post Indeed..Thanks you made my day

4/15/2009 8:20 PM | Rave
Gravatar

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

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
4/22/2009 10:46 AM | Waleed Mohamed
Gravatar

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

good article.
5/19/2009 8:16 PM | pravesh
Gravatar

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

Hi,
Great Post!! But I am getting this given error. Could anyone resolve this problem.

System.Data.NoNullAllowedException: Column 'SubjectID' does not allow nulls.
6/3/2009 4:42 AM | kumar
Gravatar

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

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();
}
6/3/2009 4:51 AM | kumar
Gravatar

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

Very good article.. doesnt need to overwrite or create any custom control unlike the rest. Efficient!!
6/26/2009 4:11 AM | ckk
Gravatar

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

hi,
Nice post
9/9/2009 6:07 PM | kesava
Gravatar

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

Nice Posting, It worked for me. Thanks!!
9/23/2009 7:39 AM | Gowri
Gravatar

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

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
10/8/2009 11:04 AM | Waleed Mohamed
Gravatar

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

Thankx It is working.
10/30/2009 7:03 PM | Pradeep Manker
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: