Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to bind empty gridview in asp.net c#.My database contains table which have no rows ,empty table ,I want to display gridview only header come from table and no records show and these table header(columns) bind in empty gridview,I want this type of design image

What I have tried:

here is my code :
C#
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db_compilerConnectionString %>"
       SelectCommand="SELECT [Column_Name], [Data_Type], [null_], [Date] FROM [tbl_field]">
   </asp:SqlDataSource>
   <asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True"
       AutoGenerateColumns="False" BackColor="#DEBA84"
       BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
       DataSourceID="SqlDataSource1" ShowFooter="True" Style="margin-left: 133px; margin-top: 49px;
       margin-right: 0px;" Height="16px" Width="782px">
       <Columns>
           <asp:TemplateField HeaderText="Column_Name" SortExpression="Column_Name">
               <EditItemTemplate>
                   <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Column_Name") %>'></asp:TextBox>
                   <asp:RequiredFieldValidator Text="*" ID="colname" ControlToValidate="txtName"
                       runat="server" ErrorMessage="Column Name is Required" ForeColor="Red">
                   </asp:RequiredFieldValidator>
               </EditItemTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text='<%# Bind("Column_Name") %>'></asp:Label>
               </ItemTemplate>
               <FooterTemplate>
                   <asp:TextBox ID="txtColName" runat="server"></asp:TextBox>
                   <asp:RequiredFieldValidator ValidationGroup="INSERT" Text="*" ID="insertcolname" ControlToValidate="txtColName"
                       runat="server" ErrorMessage="Column Name is Required" ForeColor="Red">
                   </asp:RequiredFieldValidator>
               </FooterTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Data_Type" SortExpression="Data_Type">
               <EditItemTemplate>
                   <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Data_Type") %>'>
                       <asp:ListItem>Select Datatype</asp:ListItem>
                       <asp:ListItem>int</asp:ListItem>
                       <asp:ListItem>Float</asp:ListItem>
                       <asp:ListItem>Varchar(50)</asp:ListItem>
                       <asp:ListItem>Varchar(100)</asp:ListItem>
                       <asp:ListItem>double</asp:ListItem>
                   </asp:DropDownList>
                   <asp:RequiredFieldValidator ID="datatype" ControlToValidate="DropDownList1"
                       runat="server" ErrorMessage="Data Type is Required" ForeColor="Red" InitialValue="Select Datatype">
                   </asp:RequiredFieldValidator>
               </EditItemTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label2" runat="server" Text='<%# Bind("Data_Type") %>'></asp:Label>
               </ItemTemplate>
               <FooterTemplate>
                   <asp:DropDownList ID="DropDownList2" runat="server">
                       <asp:ListItem>Select Datatype</asp:ListItem>
                       <asp:ListItem>int</asp:ListItem>
                       <asp:ListItem>Float</asp:ListItem>
                       <asp:ListItem>Varchar(50)</asp:ListItem>
                       <asp:ListItem>Varchar(100)</asp:ListItem>
                       <asp:ListItem>double</asp:ListItem>
                   </asp:DropDownList>
                   <asp:RequiredFieldValidator ValidationGroup="INSERT" Text="*"  ID="datatype" ControlToValidate="DropDownList2"
                       runat="server" ErrorMessage="Data Type is Required" ForeColor="Red" InitialValue="Select Datatype">
                   </asp:RequiredFieldValidator>
               </FooterTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="null_" SortExpression="null_">
               <EditItemTemplate>
                   <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("null_") %>' />
               </EditItemTemplate>
               <ItemTemplate>
                   <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("null_") %>' Enabled="false" />
               </ItemTemplate>
               <FooterTemplate>
                   <asp:CheckBox ID="null" runat="server" />
               </FooterTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Date" SortExpression="Date">
               <EditItemTemplate>
                   <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
               </EditItemTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label3" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
               </ItemTemplate>
               <FooterTemplate>
                   <asp:LinkButton ValidationGroup="INSERT" ID="lblInsert" runat="server">Insert</asp:LinkButton>
               </FooterTemplate>
           </asp:TemplateField>
           <asp:CommandField AccessibleHeaderText="Delete" ShowDeleteButton="True" />
           <asp:CommandField AccessibleHeaderText="Edit" ShowEditButton="True" />
       </Columns>
       <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
       <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
       <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
       <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
       <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
       <SortedAscendingCellStyle BackColor="#FFF1D4" />
       <SortedAscendingHeaderStyle BackColor="#B95C30" />
       <SortedDescendingCellStyle BackColor="#F1E5CE" />
       <SortedDescendingHeaderStyle BackColor="#93451F" />
   </asp:GridView>
   <asp:ValidationSummary ValidationGroup="INSERT" ID="ValidationSummary1" ForeColor="Red"
       runat="server" Height="44px" />
   <asp:ValidationSummary ID="ValidationSummary2" ForeColor="Red" runat="server" />
Posted
Updated 3-Apr-16 23:18pm
v4
Comments
Sinisa Hajnal 4-Apr-16 5:26am    
What have you tried? Bind the grid to the datasource and it should show you the columns with empty rows.

use ShowHeaderWhenEmpty[^]

eg:
ASP.NET
<asp:GridView ID="GridView1" ShowHeaderWhenEmpty="true" DataSourceID="SqlDataSource1" runat="server">
     </asp:GridView>
 
Share this answer
 
Comments
Hameed Khan 4-Apr-16 5:16am    
I try this but this show only headers of empty table,,i want that show footer of the gridview and header of the table as well,,actually in gridview footer ,i insert textbox,dropdownlist,and checkbox as u see in image,how can i show footer of gridview when gridview is empty
Use:

<asp:gridview.......... ShowHeaderWhenEmpty="true"........ </asp:gridview>


http://stackoverflow.com/questions/354369/gridview-show-headers-on-empty-data-source

http://www.aspdotnet-suresh.com/2010/12/v-behaviorurldefaultvmlo.html[^]
 
Share this answer
 
v2
Comments
Hameed Khan 4-Apr-16 5:19am    
I try this but this show only headers of empty table,,i want that show footer of the gridview and header of the table as well,,actually in gridview footer ,i insert textbox,dropdownlist,and checkbox as u see in image,how can i show footer of gridview when gridview is empty
deepankarbhatnagar 4-Apr-16 5:23am    
http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx

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