Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm adding a GridView & then showing data in it from a SQL Server database. The problem is that the GridView is not displaying in the browser with or without data.

<pre> <asp:GridView ID="grdLocation" runat="server" AutoGenerateColumns="False" 
         onrowdatabound="grdLocation_RowDataBound" 
         onrowdeleted="grdLocation_RowDeleted" Width="100%" CssClass="grid-view" 
         EnableModelValidation="True" ViewStateMode="Enabled">
        <Columns>   
            <asp:TemplateField HeaderText="Location Name">
                <ItemTemplate>
                    <asp:Label ID="lblShowLocationName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditLocationName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvLocationName" runat="server" ControlToValidate="txtEditLocationName" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="LocationUpdate"></asp:RequiredFieldValidator>
                    <%--<asp:RegularExpressionValidator ID="rexvLocationNameEdit" runat="server" ControlToValidate="txtEditLocationName" SetFocusOnError="true" ValidationGroup="Location" ValidationExpression="^[a-zA-Z ]*$" ErrorMessage="Only Alphabets Allowed" Display="Dynamic"></asp:RegularExpressionValidator>--%>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Location Code">
                <ItemTemplate>
                    <asp:Label ID="lblShowLocationCode" runat="server" Text='<%# Eval("Code") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditLocationCode" runat="server" Text='<%# Eval("Code") %>' 
                        ></asp:TextBox>
                    
                </EditItemTemplate>
                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="imgBtnshow" runat="server" ToolTip="View" 
                        CommandArgument='<%# Eval("LocationId") %>' ImageUrl="~/Images/file_edit.png"
                        onclick="imgBtnshow_Click" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:ImageButton ID="imgBtnEdit" runat="server" CommandArgument='<%# Eval("LocationId") %>' ImageUrl="~/Images/buttonsave.png" OnClick="imgBtnEdit_Click" ValidationGroup="LocationUpdate" ToolTip="Update" />
                    <asp:ImageButton ID="imgBtnCancel" runat="server" ToolTip="Cancel"
                    ImageUrl="~/Images/1350368827_save_delete.png" onclick="imgBtnCancel_Click"/>
                </EditItemTemplate>
                <ItemStyle Width="50px" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="imgBtnDelete" runat="server" CommandArgument='<%# Eval("LocationId") %>' ImageUrl="~/Images/delete-256.png" OnClick="imgBtnDelete_Click" OnClientClick="Confirm()" ToolTip="Delete"/>
                </ItemTemplate>
                <ItemStyle Width="50px" />
            </asp:TemplateField>
            
        </Columns>
     </asp:GridView>


What I have tried:

I dont know where is problem ,there is i think all ok in code but why this gridview is not visible when we run.
Posted
Comments
an0ther1 27-May-18 18:50pm    
Show your code used to populate the datagrid.
Deekshaa Singh Chauhan 28-May-18 2:26am    
public void FillGrid()
{
DataTable dt = Location.GetAllLocation();
grdLocation.DataSource = dt;
grdLocation.DataBind();

}
an0ther1 29-May-18 1:12am    
Please use the Reply button when replying, the person leaving the comment will be notified.
Have you checked you actually have data in your DataTable? Use your debugger & confirm.
DataGrids that have no data will not display unless ShowHeaderWhenEmpty is set to true

Kind Regards
Deekshaa Singh Chauhan 30-May-18 1:35am    
yes i have data in data table bt when i check using debuger it not pic data from there
an0ther1 30-May-18 17:21pm    
Does your event handler for OnDataBound - grdLocation_RowDataBound - get fired? Use your debugger to check.
The GridView control is rendered as a Table in the client, if you have data & it is bound to the grid then it should display - I have used it hundreds of times without problem.
Once the page is rendered (visible on the client) what is in the HTML (use F12 in Chrome or IE to view the developer tools)?
Additionally, I would try making it break - change the databinding to a column that you know doesn't exist - change "Name" to "Nqme" for instance - do you get an error that the column cannot be found

Kind Regards

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