Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir.
i took one gridview. with two columns like below. i want to display these two columns when ever i load the page. even i took one label also it is not displaying.
i want to display this gridview when ever i open the page empty. to do this task what can i do. if any body know plz help me.
thanking you.

sateesh.



XML
<asp:GridView ID="GV" runat="server" AutoGenerateColumns ="false" >
            <Columns>
                <asp:TemplateField Visible="true" HeaderText ="ID">
                    <ItemTemplate>
                        <asp:Label ID="id" runat="server" Text="<%#Eval("123")%>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText ="Name" Visible ="true" >
                    <ItemTemplate >
                        <asp:Label ID="Name" runat="server" Text="<%#Eval("123")%>"> </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
Posted

1 solution

Sure.

If you notice the code of how GridView is populated, you will find that you get a dataset/datatable and assign that as a datasource to grid. After which you call a Databind method of it.

Internally, data is binded and Rowdatabound is called that many number of times as much number of rows returned in the datatable. This in return shows you those rows.

Now, at your page load or any given scenario, if the datatable assigned to the grid is having ZERO rows in it then there would be no binding and thus no display of grid or headers etc.

In order to handle this scenario such that you still see headers of grid or some label in it, you need to write a little extra code from your side. Steps:
1. Before assigning the datasource to grid, check for number of rows in the table
2. If it's more than zero then go with normal flow
3. If it's zero then create a dummy table from your side, add a dummy row with no values or some text in it
4. Assign this new table to grid and call databind method.

Done! Try.
 
Share this answer
 
Comments
TweakBird 27-Dec-10 12:43pm    
Good call Sandeep.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900