Click here to Skip to main content
12,747,154 members (29,124 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 8 Feb 2007

Show GridView even if datasource is empty

, 8 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple method to show a GridView even if the datasource asigned is empty.


I was recently faced with a problem where I used a GridView to add new data directly into the grid from the grid's footer, but found out that when there is no data (no rows in my DataTable) to show initially, the grid was not visible, and thus adding new data via the footer was not possible.

Solution Explanation

I wrote a simple method that adds an empty row to the GridView's DataSource then hides that row. Many examples out there shows this technique using the RowDataBound event; it works initially, but when doing a second postback, the empty row and its controls will show up again.

The Code

The method below takes the grid's DataSource and extracts the DataTable from it; it clones a copy and then adds an empty row. Cloning the DataTable will prevent you from adding an empty row inadvertently to your "Master" source.

You will notice two if statements; the first one will execute directly after you have assigned a source to your GridView, the second will execute on Page_Load.

/// =============================================================
/// <summary>
/// Show grid even if datasource is empty
/// <param name="grdView">GridView</param>
/// =============================================================
protected void EmptyGridFix(GridView grdView)
    // normally executes after a grid load method
    if (grdView.Rows.Count == 0 &&
        grdView.DataSource != null)
        DataTable dt = null;
        // need to clone sources otherwise
        // it will be indirectly adding to 
        // the original source
        if (grdView.DataSource is DataSet)
            dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
        else if (grdView.DataSource is DataTable)
            dt = ((DataTable)grdView.DataSource).Clone();

        if (dt == null)
        dt.Rows.Add(dt.NewRow()); // add empty row
        grdView.DataSource = dt;

        // hide row
        grdView.Rows[0].Visible = false;

    // normally executes at all postbacks
    if (grdView.Rows.Count == 1 &&
        grdView.DataSource == null)
        bool bIsGridEmpty = true;
        // check first row that all cells empty
        for (int i = 0; i < grdView.Rows[0].Cells.Count; i++)
            if (grdView.Rows[0].Cells[i].Text != string.Empty)
                bIsGridEmpty = false;
        // hide row
        if (bIsGridEmpty)
            grdView.Rows[0].Visible = false;


The method must be called on two occasions:

  1. Directly after assigning your DataSet or DataTable to your GridView
  2. On each Page_Load
public class MyPage : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!this.IsPostBack)

    protected void LoadGrid()
        DataSet dsMyDataSet = null;

        // obtain dataset/datatable from DAL/BAL

        grdYourGrid.DataSource = dsMyDataSet.Table[0];


    protected void EmptyGridFix(GridView grdView)
        // method code comes here

    // rest of page implementation would follow

Last words

The code above only works on instances where the GridView DataSource is being populated via a DataSet or DataTable, I have not tested any other sources.

A good idea is to create a class that would inherit from System.Web.UI.Page and drop the above method in there and have your pages inherit from that class.

I guess there are cleaner/better ways to implement this if... I can think of some really outrageous ways of fixing this stupid problem, like overloading the original GridView control. But one has to weigh the effort spent against the benefits. I really hope to see a fix in future versions of .NET.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
South Africa South Africa
My name is Barend Bootha, I'm a C#/Web Developer for a big media firm in South Africa, previously I was involved with financial systems programming and also big CMS's for government contracts.

You may also be interested in...


Comments and Discussions

QuestionWorked like a charm! Pin
FieryPhoenix20126-Mar-13 10:46
memberFieryPhoenix20126-Mar-13 10:46 
GeneralMy vote of 5 Pin
Inba karthik10-Jan-13 23:42
memberInba karthik10-Jan-13 23:42 
QuestionWhat to do in this case if i use DAL method that returns List<ObjectBLL>? Pin
x48ray5-Apr-10 7:02
memberx48ray5-Apr-10 7:02 
GeneralUseless Pin
Kbog28-Jan-10 4:43
memberKbog28-Jan-10 4:43 
GeneralRe: Useless Pin
johnsaxton@yahoo.com9-Mar-10 16:11
memberjohnsaxton@yahoo.com9-Mar-10 16:11 
GeneralShow GridView if datasource is empty Pin
ShashikantHadgal10-Feb-09 0:27
memberShashikantHadgal10-Feb-09 0:27 
GeneralColumn 'XXXX' does not allow nulls. Pin
Mahir7827-Sep-08 4:49
memberMahir7827-Sep-08 4:49 
GeneralDynamically add a new row to a existing GridView Pin
SRKVELLANKI19-Sep-08 6:04
memberSRKVELLANKI19-Sep-08 6:04 
GeneralError in DropDownList SelectedValue Pin
gustavoadt10-Sep-07 11:09
membergustavoadt10-Sep-07 11:09 
GeneralReally its very good... Pin
pujari.sreenivas13-Aug-07 1:09
memberpujari.sreenivas13-Aug-07 1:09 
GeneralCan't seem to set it to work Pin
igis17-Jul-07 11:23
memberigis17-Jul-07 11:23 
GeneralRe: Can't seem to set it to work Pin
khan_SharePoint21-Nov-07 23:42
memberkhan_SharePoint21-Nov-07 23:42 
GeneralJust a comment.... Pin
ShirleySW27-Feb-07 13:49
memberShirleySW27-Feb-07 13:49 
QuestionWhat about declarative data sources? Pin
awarberg17-Feb-07 6:56
memberawarberg17-Feb-07 6:56 
AnswerRe: What about declarative data sources? Pin
Vl@de18-Feb-07 18:44
memberVl@de18-Feb-07 18:44 
QuestionIs this method really needed? Pin
ilog9-Feb-07 6:19
memberilog9-Feb-07 6:19 
AnswerRe: Is this method really needed? Pin
ilog9-Feb-07 6:23
memberilog9-Feb-07 6:23 
GeneralRe: Is this method really needed? Pin
txghia589-Feb-07 6:43
membertxghia589-Feb-07 6:43 
AnswerRe: Is this method really needed? Pin
Vl@de9-Feb-07 18:43
memberVl@de9-Feb-07 18:43 
GeneralRe: Is this method really needed? Pin
Pallavi Bhoite25-Jul-07 20:36
memberPallavi Bhoite25-Jul-07 20:36 
GeneralRe: Is this method really needed? Pin
lvarney4-Feb-09 8:29
memberlvarney4-Feb-09 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 9 Feb 2007
Article Copyright 2007 by Vl@de
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid