GridView not Showing Headers When there are No Rows

  • Thread starter Thread starter Steve Harclerode
  • Start date Start date
S

Steve Harclerode

Hi,

I'm trying to get my DataGrid to show headers and footers when no rows are
returned from the datasource. Below is code that shows the first
TemplateField. Is there an easy way to get at least the footer to show? I
want to be able to insert a new row using the footer. Thanks...

<asp:GridView ID="gvMaterials" runat="server"
AutoGenerateColumns="False"
DataSourceID="dsMaterials" ShowFooter="True" Width="100%"
AutoGenerateEditButton="True">
<Columns>
<asp:TemplateField HeaderText="Material:">
<FooterTemplate>
<asp:TextBox ID="txtDescription"
runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server"
Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%#
Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
[etc...]
 
What is the actual data-source - i.e. the object you give to
dsMaterials?

I'm going to go out on a limb and guess that it is an ArrayList?

To get metadata, it needs some clue to the data. There are various
ways it can do this - the simplest is a typed indexer - i.e. a "public
T this[int index] {get;}" for some T (doesn't have to be generics...).

In the absense of a typed indexer, the metadata of the first item is
used. If there aren't any items, you're scuppered.

(there are also things like ITypedList and IListSource that are taken
into account, but these are more complex)
 
Oops; I forgot to say! If you are using an ArrayList, the easiest way
to fix this is to use a List<T> or BindingList<T>, or perhaps even
just an array: T[]

If you aren't using ArrayList, then tell us what you *are* using as
the data-source.

Marc
 
I'm using a SqlDataSource that calls a stored procedure -- which contains a
simple "SELECT * FROM Tablex WHERE ...".

Thanks,
Steve
 
Hmmm... I would have expected to work. I'm sorry, but I don't know. If
you don't get any other takers, I would recommend posting on an asp
group, since there will be many GridView experts there...

Marc
 
HI Steve,

Gridview does not display anything if database is returning zero number of rows.
In case you still want to display it then you can enter some dummy rows in an arraylist or dataset and then connect it to gridview and then hide that dummy row in grid , it will display only header and footer now.
 
Back
Top