Kevin Marshall's Epic Work Blog for Awesome People

@ksmarshall - i <3 software
in

Displaying GridView When No Data Exists

One thing thats midly annoying to me about the new gridview control is that it doesn't display the header row when there is no data.  I would like to just have the header displayed like the original table with a single row saying something like "No records found".  The grid has a property for emptydata text which doesn't look that great in my opinion.  I have seen some people use the <EmptyDataTemplate> property to create a new table in there which isn't the cleanest solution.  One way to get the gridview to display how you want is to create a custom control that inherts from gidview.  Then you can override the CreateChildControls method to create a the gridview with the all the headers and a blank row with one cell and a text message.  Here's how to do it:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public class EmptyGridView : GridView
{
    #region Properties

    /// <summary>
    /// Enable or Disable generating an empty table if no data rows in source
    /// 
</summary>
    [
    Description("Enable or disable generating an empty table with headers if no data rows in source"),
    Category("Misc"),
    DefaultValue("true"),
    ]
    public bool ShowEmptyTable
    {
        get
        {
            object o = ViewState["ShowEmptyTable"];
            return (o != null ? (bool)o : true);
        }
        set
        {
            ViewState["ShowEmptyTable"] = value;
        }
    }
    
    /// 
<summary>
    /// Get or Set Text to display in empty data row
    /// 
</summary>
    [
    Description("Text to display in empty data row"),
    Category("Misc"),
    DefaultValue(""),
    ]
    public string EmptyTableRowText
    {
        get
        {
            object o = ViewState["EmptyTableRowText"];
            return (o != null ? o.ToString() : "");
        }
        set
        {
            ViewState["EmptyTableRowText"] = value;
        }
    }

    #endregion


    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
    {
        int numRows = base.CreateChildControls(dataSource, dataBinding);

        //no data rows created, create empty table if enabled
        if (numRows == 0 && ShowEmptyTable)
        {
            //create table
            Table table = new Table();
            table.ID = this.ID;

            //create a new header row
            GridViewRow row = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

            //convert the exisiting columns into an array and initialize
            DataControlField[] fields = new DataControlField[this.Columns.Count];
            this.Columns.CopyTo(fields, 0);
            this.InitializeRow(row, fields);
            table.Rows.Add(row);

            //create the empty row
            row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
            TableCell cell = new TableCell();
            cell.ColumnSpan = this.Columns.Count;
            cell.Width = Unit.Percentage(100);
            cell.Controls.Add(new LiteralControl(EmptyTableRowText));
            row.Cells.Add(cell);
            table.Rows.Add(row);

            this.Controls.Add(table);
        }

        return numRows;
    }
}

You can download the code here. Reflector was helpful in seeing how grids were created in the framework as was Fredrik Normen's blog which always has great asp.net info.  Everytime i search for asp.net problem, its usually covered on his site.

Posted: Feb 28 2006, 12:21 PM by kmarshall | with 30 comment(s)
Filed under:

Comments

Kevin Koehne said:

Thanks for posting this. It was exactly what I was looking for. One minor tweak:
table.CssClass = this.CssClass;
# January 10, 2008 5:12 PM

dinu selva, chennai said:

hi marshall,

I got error : CreateChildControls(System.Collections.IEnumerable, bool)': no suitable method found to override.

how i ll solve this?

# February 1, 2008 5:13 AM

kmarshall said:

Dinu,

Could you be a little more specific?  Are you inheriting from GridView?  

# February 5, 2008 2:55 AM

Eric said:

Hi,

Thanks for sharing the code with us; I've found your EmptyGridView class implemention really helpful.

I am  developing a web application and I want to implement an insert row in the footer to allow the creation of new records.  I ran into a minor issue as the "FindControl" for the FooterRow property fails to location the textboxes, dropdownlist, etc in my footer template.  As such, I have made some modifications to handle such a situtation and would like to share the code with you.

<!-- code formatted by manoli.net/csharpformat -->

<style type="text/css">

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: Consolas, "Courier New", Courier, Monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

</style>

<div class="csharpcode">

<pre><span class="lnum">   1:  </span><span class="kwrd">using</span> System;</pre>

<pre><span class="lnum">   2:  </span><span class="kwrd">using</span> System.Collections.Generic;</pre>

<pre><span class="lnum">   3:  </span><span class="kwrd">using</span> System.ComponentModel;</pre>

<pre><span class="lnum">   4:  </span><span class="kwrd">using</span> System.Text;</pre>

<pre><span class="lnum">   5:  </span><span class="kwrd">using</span> System.Web.UI;</pre>

<pre><span class="lnum">   6:  </span><span class="kwrd">using</span> System.Web.UI.WebControls;</pre>

<pre><span class="lnum">   7:  </span>&nbsp;</pre>

<pre><span class="lnum">   8:  </span><span class="kwrd">namespace</span> com.picanada.intranet.ui.controls</pre>

<pre><span class="lnum">   9:  </span>{</pre>

<pre><span class="lnum">  10:  </span>&nbsp;</pre>

<pre><span class="lnum">  11:  </span>  <span class="kwrd">public</span> <span class="kwrd">class</span> GridViewWithInsertRow : GridView</pre>

<pre><span class="lnum">  12:  </span>  {</pre>

<pre><span class="lnum">  13:  </span>    <span class="kwrd">protected</span> Table table = <span class="kwrd">new</span> Table();</pre>

<pre><span class="lnum">  14:  </span>&nbsp;</pre>

<pre><span class="lnum">  15:  </span>   </pre>

<pre><span class="lnum">  16:  </span>    <span class="preproc">#region</span> Properties</pre>

<pre><span class="lnum">  17:  </span>&nbsp;</pre>

<pre><span class="lnum">  18:  </span>    <span class="rem">/// &lt;summary&gt;</span></pre>

<pre><span class="lnum">  19:  </span>    <span class="rem">/// Enable or Disable generating an empty table if no data rows in source</span></pre>

<pre><span class="lnum">  20:  </span>    <span class="rem">/// &lt;/summary&gt;</span></pre>

<pre><span class="lnum">  21:  </span>    [</pre>

<pre><span class="lnum">  22:  </span>    Description(<span class="str">"Enable or disable generating an empty table with headers if no data rows in source"</span>),</pre>

<pre><span class="lnum">  23:  </span>    Category(<span class="str">"Misc"</span>),</pre>

<pre><span class="lnum">  24:  </span>    DefaultValue(<span class="str">"true"</span>),</pre>

<pre><span class="lnum">  25:  </span>    ]</pre>

<pre><span class="lnum">  26:  </span>    <span class="kwrd">public</span> <span class="kwrd">bool</span> ShowEmptyTable</pre>

<pre><span class="lnum">  27:  </span>    {</pre>

<pre><span class="lnum">  28:  </span>      get</pre>

<pre><span class="lnum">  29:  </span>      {</pre>

<pre><span class="lnum">  30:  </span>        <span class="kwrd">object</span> o = ViewState[<span class="str">"ShowEmptyTable"</span>];</pre>

<pre><span class="lnum">  31:  </span>        <span class="kwrd">return</span> (o != <span class="kwrd">null</span> ? (<span class="kwrd">bool</span>)o : <span class="kwrd">true</span>);</pre>

<pre><span class="lnum">  32:  </span>      }</pre>

<pre><span class="lnum">  33:  </span>      set</pre>

<pre><span class="lnum">  34:  </span>      {</pre>

<pre><span class="lnum">  35:  </span>        ViewState[<span class="str">"ShowEmptyTable"</span>] = <span class="kwrd">value</span>;</pre>

<pre><span class="lnum">  36:  </span>      }</pre>

<pre><span class="lnum">  37:  </span>    }</pre>

<pre><span class="lnum">  38:  </span>&nbsp;</pre>

<pre><span class="lnum">  39:  </span>&nbsp;</pre>

<pre><span class="lnum">  40:  </span>    <span class="rem">/// &lt;summary&gt;</span></pre>

<pre><span class="lnum">  41:  </span>    <span class="rem">/// Get or Set Text to display in empty data row</span></pre>

<pre><span class="lnum">  42:  </span>    <span class="rem">/// &lt;/summary&gt;</span></pre>

<pre><span class="lnum">  43:  </span>    [</pre>

<pre><span class="lnum">  44:  </span>    Description(<span class="str">"Text to display in empty data row"</span>),</pre>

<pre><span class="lnum">  45:  </span>    Category(<span class="str">"Misc"</span>),</pre>

<pre><span class="lnum">  46:  </span>    DefaultValue(<span class="str">""</span>),</pre>

<pre><span class="lnum">  47:  </span>    ]</pre>

<pre><span class="lnum">  48:  </span>    <span class="kwrd">public</span> <span class="kwrd">string</span> EmptyTableRowText</pre>

<pre><span class="lnum">  49:  </span>    {</pre>

<pre><span class="lnum">  50:  </span>      get</pre>

<pre><span class="lnum">  51:  </span>      {</pre>

<pre><span class="lnum">  52:  </span>        <span class="kwrd">object</span> o = ViewState[<span class="str">"EmptyTableRowText"</span>];</pre>

<pre><span class="lnum">  53:  </span>        <span class="kwrd">return</span> (o != <span class="kwrd">null</span> ? o.ToString() : <span class="str">""</span>);</pre>

<pre><span class="lnum">  54:  </span>      }</pre>

<pre><span class="lnum">  55:  </span>      set</pre>

<pre><span class="lnum">  56:  </span>      {</pre>

<pre><span class="lnum">  57:  </span>        ViewState[<span class="str">"EmptyTableRowText"</span>] = <span class="kwrd">value</span>;</pre>

<pre><span class="lnum">  58:  </span>      }</pre>

<pre><span class="lnum">  59:  </span>    }</pre>

<pre><span class="lnum">  60:  </span>&nbsp;</pre>

<pre><span class="lnum">  61:  </span>    <span class="preproc">#endregion</span></pre>

<pre><span class="lnum">  62:  </span>&nbsp;</pre>

<pre><span class="lnum">  63:  </span>    </pre>

<pre><span class="lnum">  64:  </span>    <span class="kwrd">public</span> <span class="kwrd">new</span> GridViewRow HeaderRow</pre>

<pre><span class="lnum">  65:  </span>    {</pre>

<pre><span class="lnum">  66:  </span>      get</pre>

<pre><span class="lnum">  67:  </span>      {</pre>

<pre><span class="lnum">  68:  </span>        <span class="kwrd">return</span> (table.FindControl(<span class="str">"HeaderRow"</span>) <span class="kwrd">as</span> GridViewRow == <span class="kwrd">null</span>) ? <span class="kwrd">base</span>.HeaderRow : table.FindControl(<span class="str">"HeaderRow"</span>) <span class="kwrd">as</span> GridViewRow;</pre>

<pre><span class="lnum">  69:  </span>      }</pre>

<pre><span class="lnum">  70:  </span>    }</pre>

<pre><span class="lnum">  71:  </span>&nbsp;</pre>

<pre><span class="lnum">  72:  </span>    <span class="kwrd">public</span> <span class="kwrd">new</span> GridViewRow FooterRow</pre>

<pre><span class="lnum">  73:  </span>    {</pre>

<pre><span class="lnum">  74:  </span>      get</pre>

<pre><span class="lnum">  75:  </span>      {</pre>

<pre><span class="lnum">  76:  </span>        <span class="kwrd">return</span> (table.FindControl(<span class="str">"FooterRow"</span>) <span class="kwrd">as</span> GridViewRow == <span class="kwrd">null</span>) ? <span class="kwrd">base</span>.FooterRow : table.FindControl(<span class="str">"FooterRow"</span>) <span class="kwrd">as</span> GridViewRow;</pre>

<pre><span class="lnum">  77:  </span>      }</pre>

<pre><span class="lnum">  78:  </span>    }</pre>

<pre><span class="lnum">  79:  </span>    </pre>

<pre><span class="lnum">  80:  </span>&nbsp;</pre>

<pre><span class="lnum">  81:  </span>    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">int</span> CreateChildControls(System.Collections.IEnumerable dataSource, <span class="kwrd">bool</span> dataBinding)</pre>

<pre><span class="lnum">  82:  </span>    {</pre>

<pre><span class="lnum">  83:  </span>      <span class="kwrd">int</span> numRows = <span class="kwrd">base</span>.CreateChildControls(dataSource, dataBinding);</pre>

<pre><span class="lnum">  84:  </span>&nbsp;</pre>

<pre><span class="lnum">  85:  </span>      <span class="rem">//no data rows created, create empty table if enabled</span></pre>

<pre><span class="lnum">  86:  </span>      <span class="kwrd">if</span> (numRows == 0 &amp;&amp; ShowEmptyTable)</pre>

<pre><span class="lnum">  87:  </span>      {</pre>

<pre><span class="lnum">  88:  </span>        <span class="rem">//create table</span></pre>

<pre><span class="lnum">  89:  </span>        table = <span class="kwrd">new</span> Table();</pre>

<pre><span class="lnum">  90:  </span>        table.ID = <span class="kwrd">this</span>.ID;</pre>

<pre><span class="lnum">  91:  </span>&nbsp;</pre>

<pre><span class="lnum">  92:  </span>        GridViewRow row;</pre>

<pre><span class="lnum">  93:  </span>&nbsp;</pre>

<pre><span class="lnum">  94:  </span>        <span class="rem">//create a new header row</span></pre>

<pre><span class="lnum">  95:  </span>        <span class="kwrd">if</span> (<span class="kwrd">this</span>.ShowHeader &amp;&amp; (<span class="kwrd">this</span>.HeaderRow != <span class="kwrd">null</span>))</pre>

<pre><span class="lnum">  96:  </span>        {</pre>

<pre><span class="lnum">  97:  </span>          row = <span class="kwrd">base</span>.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);</pre>

<pre><span class="lnum">  98:  </span>&nbsp;</pre>

<pre><span class="lnum">  99:  </span>          <span class="rem">//convert the exisiting columns into an array and initialize</span></pre>

<pre><span class="lnum"> 100:  </span>          DataControlField[] fields = <span class="kwrd">new</span> DataControlField[<span class="kwrd">this</span>.Columns.Count];</pre>

<pre><span class="lnum"> 101:  </span>          <span class="kwrd">this</span>.Columns.CopyTo(fields, 0);</pre>

<pre><span class="lnum"> 102:  </span>          <span class="kwrd">this</span>.InitializeRow(row, fields);</pre>

<pre><span class="lnum"> 103:  </span>          row.ID = <span class="str">"HeaderRow"</span>;</pre>

<pre><span class="lnum"> 104:  </span>          table.Rows.Add(row);</pre>

<pre><span class="lnum"> 105:  </span>        }</pre>

<pre><span class="lnum"> 106:  </span>&nbsp;</pre>

<pre><span class="lnum"> 107:  </span>        <span class="rem">//create the empty row</span></pre>

<pre><span class="lnum"> 108:  </span>        row = <span class="kwrd">new</span> GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);</pre>

<pre><span class="lnum"> 109:  </span>        TableCell cell = <span class="kwrd">new</span> TableCell();</pre>

<pre><span class="lnum"> 110:  </span>        cell.ColumnSpan = <span class="kwrd">this</span>.Columns.Count;</pre>

<pre><span class="lnum"> 111:  </span>        cell.Width = Unit.Percentage(100);</pre>

<pre><span class="lnum"> 112:  </span>        cell.Controls.Add(<span class="kwrd">new</span> LiteralControl(EmptyTableRowText));</pre>

<pre><span class="lnum"> 113:  </span>        row.Cells.Add(cell);</pre>

<pre><span class="lnum"> 114:  </span>        table.Rows.Add(row);</pre>

<pre><span class="lnum"> 115:  </span>&nbsp;</pre>

<pre><span class="lnum"> 116:  </span>&nbsp;</pre>

<pre><span class="lnum"> 117:  </span>        <span class="kwrd">if</span> (<span class="kwrd">this</span>.ShowFooter &amp;&amp; (<span class="kwrd">this</span>.FooterRow != <span class="kwrd">null</span>))</pre>

<pre><span class="lnum"> 118:  </span>        {</pre>

<pre><span class="lnum"> 119:  </span>          row = <span class="kwrd">base</span>.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);</pre>

<pre><span class="lnum"> 120:  </span>&nbsp;</pre>

<pre><span class="lnum"> 121:  </span>          <span class="rem">//convert the exisiting columns into an array and initialize</span></pre>

<pre><span class="lnum"> 122:  </span>          DataControlField[] fields = <span class="kwrd">new</span> DataControlField[<span class="kwrd">this</span>.Columns.Count];</pre>

<pre><span class="lnum"> 123:  </span>          <span class="kwrd">this</span>.Columns.CopyTo(fields, 0);</pre>

<pre><span class="lnum"> 124:  </span>          <span class="kwrd">this</span>.InitializeRow(row, fields);</pre>

<pre><span class="lnum"> 125:  </span>          row.ID = <span class="str">"FooterRow"</span>;</pre>

<pre><span class="lnum"> 126:  </span>          table.Rows.Add(row);</pre>

<pre><span class="lnum"> 127:  </span>        }</pre>

<pre><span class="lnum"> 128:  </span>&nbsp;</pre>

<pre><span class="lnum"> 129:  </span>        <span class="kwrd">this</span>.Controls.Add(table);</pre>

<pre><span class="lnum"> 130:  </span>      }</pre>

<pre><span class="lnum"> 131:  </span>&nbsp;</pre>

<pre><span class="lnum"> 132:  </span>      <span class="kwrd">return</span> numRows;</pre>

<pre><span class="lnum"> 133:  </span>    }</pre>

<pre><span class="lnum"> 134:  </span>  }</pre>

<pre><span class="lnum"> 135:  </span>}</pre>

</div>

# February 27, 2008 1:35 PM

PrisLena said:

How Do I connect the above code to my gridview

Thanks

# March 17, 2008 4:25 PM

PrisLena said:

Hi

I am fairly new to ASP.Net and I would appreciate your help

I have a gridview in which I am inserting records in the footer.  I would like to display the gridview when the gridview is empty so that I can insert my data.  I am using the  first code above.  The one that inherits from a gridview i.e public class EmptyGridView : GridView.  How do I call this class so that it works with the gridview I already have

Thanks

# March 17, 2008 4:42 PM

Menno said:

Make sure that your class "EmptyGridView" got the namespace namespace EmptyGridView.

Register the class above the aspx page like <%@ Register TagPrefix="Custom" Namespace="EmptyGridView" %>

Call control:

<Custom:EmptyGridView id="GridView1", .......>

<Columns>

<asp:TemplateField>

<HeaderTemplate>

<asp:ImageButton ID="AddButton" runat="server" ImageUrl="~/Images/add.gif" CommandName="Add"/>

</HeaderTemplate>

</asp:TemplateField>

</Columns>

</Custom:EmptyGridView>

I like this solution, it's easy and workable

# May 5, 2008 7:20 AM

Yauhen said:

Great solution, Thank you!

# August 13, 2008 2:20 AM

Olivier SOW said:

i bein in asp.net and i cannot insert the custom control

i created the EmptyGridView class in the EmptyGridView namespace,

added the Register tag just below the page tag

and adding the control like this

<Custom:EmptyGridView id="GridView1", .......>

</Custom:EmptyGridView>

the designer say to me that the tag is unknown, i can compil but on runtime, i have errror, tagt unknown

i did something wrong ?

# August 22, 2008 4:40 PM

Graham said:

This looks promising, but I get the same as Olivier.

# September 5, 2008 3:55 AM

Kim said:

Brilliant! I'd already created a custom control so just popped the CreateChildControls override in and it worked a treat!

# September 16, 2008 3:52 AM

Tapps said:

Couple of notes

if using .net 3.5

move the register <%@ Register TagPrefix="Custom" Namespace="EmptyGridView" %>

entry to the web.config under controls as

<add tagPrefix="custom" namespace="EmptyGridView"  />

i also added changed it slightly  adding

     row = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);                

               this.InitializeRow(row, fields);

               table.Rows.Add(row);                

               OnRowDataBound(new GridViewRowEventArgs(row));

as the second row and the below so that it waorks as a footerrow again for adding on an empty grid.

public override GridViewRow FooterRow

       {

           get

           {

               if (!ShowEmptyTable)

                   return base.FooterRow;

               else

                   return ((Table)this.Controls[0]).Rows[1] as GridViewRow;

           }

       }  

hope it helps

# December 19, 2008 3:57 AM

Senthil said:

I need a vb.net code to make an empty row

# December 23, 2008 7:44 AM

Gridview bei Empty angezeigt lassen | hilpers said:

Pingback from  Gridview bei Empty angezeigt lassen | hilpers

# January 20, 2009 8:15 AM

show header when gridview is empty | keyongtech said:

Pingback from  show header when gridview is empty | keyongtech

# January 22, 2009 2:50 AM

Jamie said:

Great work, thank you.   Unfortunately the control doesn't return FooterRow, so I can't use FindControl to get values.   Has anyone found a fix for that?

# April 4, 2009 3:18 AM

pari said:

hi all

when i build the sample , i get this error

Missing partial modifier on declaration of type 'EmptyGridView '; another partial declaration of this type exists

# August 16, 2009 5:41 AM

Andrea said:

If you want use footer for inserting data:

if (this.ShowFooterWhenEmpty)

               {

                   //  create footer row

                   GridViewRow footerRow = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

                   _footerRow = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Edit);

                   this.InitializeRow(_footerRow, fields);

                   //  add the footer to the table

                   table.Rows.Add(_footerRow);

                   _footerRow.DataBind();

               }

# September 2, 2009 9:37 AM

Ryan said:

Hi -

I'm getting Property access must assign to the property or use it value. any thought?

# September 14, 2009 9:11 AM

PJDM said:

I have textboxes in my header columns and they become null references with this fix, anyone with more brains ready to offer up a solution? -.-)

# December 2, 2009 9:11 AM

Denis said:

Thanks a lot !

There is a difference beetwen the normal gridview and the replacement table : the ClientId is different.

I changed and added a few line of code to make the created table return the expected ClientId and also render it.

In CreateChildControls, change :

       //create table

       Table table = new Table();

       table.ID = this.ID;

by

       //create table

       GvwEmptyTable table = new GvwEmptyTable();

       table.ID = this.ID;

       table.ClientIdOverride = this.ClientID;

Add right before the end of the class :

       /// <summary>

       /// Summary description for RnoGvwEmptyTable

       /// </summary>

       private class GvwEmptyTable : System.Web.UI.WebControls.Table

       {

           /// <summary>

           /// ClientId Override, needed for empty GridView in order to get the same ClientId

           /// </summary>

           public string ClientIdOverride;

           /// <summary>

           /// Client Id Override

           /// </summary>

           public override string ClientID

           {

               get

               {

                   return ClientIdOverride.Nvl(base.ClientID);

               }

           }

       }

# December 3, 2009 2:00 AM

Sunny said:

Denis.. whatz ClientIdOverride."Nvl" ??

# December 15, 2009 4:42 PM

Mr Benn said:

Excellent piece of code, solved the problem we had where we had filters in the header, so a user could filter out all results and then not be able to change them!

Thanks!

# January 8, 2010 5:10 AM

Mas said:

Sorry Guys New to ASP

Can you let me know how to call the class to show the empty grid.

Thank

M

# January 12, 2010 2:59 PM

Peter said:

If you happen to have filter controls that has to be bound like me, call 'OnRowDataBound' event after adding a header row:

//create a new header row

               GridViewRow row = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

               //convert the exisiting columns into an array and initialize

               DataControlField[] fields = new DataControlField[this.Columns.Count];

               this.Columns.CopyTo(fields, 0);

               this.InitializeRow(row, fields);

               table.Rows.Add(row);

               OnRowDataBound(new GridViewRowEventArgs(row)); // ** This should invoke gvMine_RowDataBound(o, e) event!

Thanks heaps!

# January 19, 2010 4:17 PM

club penguin cheats said:

Unfortunately the control doesn't return FooterRow, so I can't use FindControl to get values.   Has anyone found a fix for that?

# April 13, 2010 8:00 PM

Ghayas said:

VB Code for that same,

it will work

Imports System

Imports Microsoft.VisualBasic

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Text

Imports System.Web.UI

Imports System.Web.UI.WebControls

Public Class EmptyGridView

   Inherits GridView

   Public Property ShowEmptTable() As Boolean

       Get

           Dim o As Object = ViewState("ShowEmptyTable")

           Return IIf(o <> "", CBool(o), True)

       End Get

       Set(ByVal value As Boolean)

           ViewState("ShowEmptyTable") = value

       End Set

   End Property

   Public Property EmptyTableRowText() As String

       Get

           Dim o As Object = ViewState("ShowEmptyTable")

           Return IIf(o <> "", o.ToString, "")

       End Get

       Set(ByVal value As String)

           ViewState("ShowEmptyTable") = value

       End Set

   End Property

   Protected Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer

       Dim numRows As Integer = MyBase.CreateChildControls(dataSource, dataBinding)

       If numRows = 0 And ShowEmptTable Then

           Dim table As New Table

           Dim row As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)

           Dim fields(Columns.Count) As DataControlField

           Columns.CopyTo(fields, 0)

           InitializeRow(row, fields)

           table.Rows.Add(row)

           row = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)

           Dim cell As New TableCell

           cell.ColumnSpan = Columns.Count

           cell.Width = Unit.Percentage(100)

           cell.Controls.Add(New LiteralControl(EmptyTableRowText))

           row.Cells.Add(cell)

           table.Rows.Add(row)

           Controls.Add(table)

       End If

       Return numRows

   End Function

End Class

# April 20, 2010 10:15 AM

Ghayas said:

can u plz help its show error that custom tag he not available

# April 20, 2010 10:27 AM

Zion said:

@Ghayas tanks to the vb code you given. that help a lot :)

i have go through the solution gived by Menno,but the same error come out that can`t found the custom tags.

who can help with this question?

# April 30, 2010 1:29 AM

Charles Wedyke said:

Thanks to Kevin Marshall for pointing us in the right direction. The converted VB function posted above is junk and the main authors C# function can throw NullReferenceException because of the - InitializeRow(row, fields) - statement when there are any null columns in the fields array. Below is a working version of the main pages method converted to VB:

Imports System

Imports System.Web.UI

Imports System.Web.UI.WebControls

Public Class EmptyGridView

   Inherits GridView

   ''' <summary>

   ''' Enable or Disable generating an empty table if no data rows in source

   ''' </summary>

   Public Property ShowEmptyTable() As Boolean

       Get

           Dim o As Object = ViewState("ShowEmptyTable")

           Return IIf(o Is Nothing, True, CBool(o))

       End Get

       Set(ByVal value As Boolean)

           ViewState("ShowEmptyTable") = value

       End Set

   End Property

   ''' <summary>

   ''' Get or Set Text to display in empty data row

   ''' </summary>

   Public Property EmptyTableRowText() As String

       Get

           Dim o As Object = ViewState("EmptyTableRowText")

           'Return IIf(o Is Nothing, "", o.ToString()) 'throws null reference exception because VB rocks

           If o Is Nothing Then

               Return ""

           Else

               Return o.ToString()

           End If

       End Get

       Set(ByVal value As String)

           ViewState("EmptyTableRowText") = value

       End Set

   End Property

   Protected Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer

       Dim numRows As Integer = MyBase.CreateChildControls(dataSource, dataBinding)

       If numRows = 0 And ShowEmptyTable Then

           Dim table As New Table

           table.ID = Me.ID

           Dim row As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)

           Dim fields(Me.Columns.Count) As DataControlField

           MyBase.Columns.CopyTo(fields, 0)

           Dim noNullsFields() As DataControlField = RemoveNullsFromDataControlField(fields)

           MyBase.InitializeRow(row, noNullsFields)

           table.Rows.Add(row)

           row = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)

           Dim cell As New TableCell

           cell.ColumnSpan = Me.Columns.Count

           cell.Width = Unit.Percentage(100)

           cell.Controls.Add(New LiteralControl(EmptyTableRowText))

           row.Cells.Add(cell)

           table.Rows.Add(row)

           Me.Controls.Add(table)

       End If

       Return numRows

   End Function

   Private Function RemoveNullsFromDataControlField(ByVal fields() As DataControlField) As DataControlField()

       Dim numFields As Integer

       For Each field As DataControlField In fields

           If Not field Is Nothing Then

               numFields = numFields + 1

           End If

       Next

       numFields = numFields - 1

       Dim noNullsFields(numFields) As DataControlField

       Dim i As Integer

       For i = 0 To numFields

           noNullsFields(i) = fields(i)

       Next

       Return noNullsFields

   End Function

End Class

# June 10, 2010 11:40 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)