Click to See Complete Forum and Search --> : GridView case
Martinez
March 8th, 2007, 05:13 AM
When GridView is filled with 0 rows, it becomes invisible.
I would like to see headers of all columns, even if dataSource produces 0 rows into GridView.
How to make it work?
Thank you very much in advance
mcmcom
March 8th, 2007, 11:34 AM
There is a property called GridView.EmptyDataTemplate.
You can make a template that will display whatever you want when the gridview is empty.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
hth,
mcm
Martinez
March 9th, 2007, 06:28 AM
Your link was extremaly helpfull, solving my problem completely.
Unfortunatelly, the solution provided by MSDN doesn`t work... or I`m making a mistake somewhere. The problem is, that when GridView is supplied with no records from the database, then completely nothing (regarding this GridView - any columns, headers, text, anything) is shown.
Here`s code of my GridView:
-----------------------------------------------
<asp:GridView ID="GridViewMain" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="ObjectDataSourceMainView" Width="100%" EmptyDataText="aa">
<Columns>
...
</Columns>
<EmptyDataTemplate>
<asp:Label Text="No data found." runat="server"></asp:Label>
No Data Found.
</EmptyDataTemplate>
</asp:GridView>
This is very simple problem and, regarding MSDN, my code MUST work! Any idea why it doesn`t?
Charu0306
March 9th, 2007, 07:15 AM
It shows the label with "no data found". Thast the point. The template is there to show that there were no records found and add your customre message ther einstead. IF you do MUST show to the user a grid when there are no records to display , try this:
Before assiging the dataset to the Gridview, try checking if its record count is more than 0. If not, then select a condition for the SQL query so it evaluates to NO records.
For ex,
SELECT [ProductName], [UnitPrice], [UnitsInStock], [CategoryName] FROM [Alphabetical List of Products]
If hat returns NO records, then before assigning it as Gridview DataSOurce, change its SQL query to:
SELECT [ProductName], [UnitPrice], [UnitsInStock], [CategoryName] FROM [Alphabetical List of Products] where [ProductID] = -1
- This ylelds no records but it will show the grid.
Charu0306
March 9th, 2007, 07:20 AM
Oops! It still deletes the grid view. I guess you'll have to stick to showing a message there logically ! :)
Martinez
March 9th, 2007, 07:50 AM
Oops! It still deletes the grid view. I guess you'll have to stick to showing a message there logically ! :)
Yeah, exactly! But how?
Charu0306
March 9th, 2007, 08:08 AM
You can right click on grid view , select Edit Templates choose Empty Template abd put in a message there...
It shud look like this
<EmptyDataTemplate>
Sorry! No records found.
</EmptyDataTemplate>
Martinez
March 9th, 2007, 08:18 AM
Sure, here it is:
<EmptyDataTemplate>
<asp:Label ID="ErrorLabel" Text="Brak rekordow" runat="server"></asp:Label>
No Data Found.
</EmptyDataTemplate>
Charu0306
March 9th, 2007, 05:53 PM
Why do you need a label control there? Just type in your message and execute, it should work!
Martinez
March 12th, 2007, 04:55 AM
I know it should but it doesn`t. Thats why I tried with a label, but it doesn`t work aswell. Any more ideas?
pguthi
April 8th, 2009, 06:12 AM
hi,
here i am sending code in c#
for empty grid view with headers..............
fill your data into dataset through queries.
ds = new DataSet();
da.Fill(ds);
dt = new DataTable();
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
dt.Columns.Add(ds.Tables[0].Columns[j].ColumnName.ToString());
}
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dr = dt.NewRow();
for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
{
dr[k] = ds.Tables[0].Rows[i][k];
}
dt.Rows.Add(dr);
}
}
else
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
GridView gv = new GridView();
gv.DataSource = dt;
gv.DataBind();
this.form1.Controls.Add(gv);
May be this will helps you.
pguthi
April 8th, 2009, 06:15 AM
hi,
did you find the solution?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.