GridView - Show headers on empty data source

后端 未结 16 1091
挽巷
挽巷 2020-11-28 11:22

In C# how do I still show the headers of a gridview, even with the data source is empty.

I am not auto generating the columns as they are all predefined.

C

相关标签:
16条回答
  • 2020-11-28 12:00

    You may use EmptyDataText as shown below:

    <asp:GridView ID="_gridView" RunAt="server" AutoGenerateColumns="false"
              EmptyDataText="No entries found.">
    

    It does not show headers, it renders your message "No entries found." instead.

    0 讨论(0)
  • 2020-11-28 12:01

    Add this property to your grid-view : ShowHeaderWhenEmpty="True" it might help just check

    0 讨论(0)
  • 2020-11-28 12:03
    <asp:GridView ID="gvEmployee" runat="server"    
                     AutoGenerateColumns="False" ShowHeaderWhenEmpty=”True”>  
                        <Columns>  
                            <asp:BoundField DataField="Id" HeaderText="Id" />  
                            <asp:BoundField DataField="Name" HeaderText="Name" />  
                            <asp:BoundField DataField="Designation" HeaderText="Designation" />  
                            <asp:BoundField DataField="Salary" HeaderText="Salary"  />  
                        </Columns>  
                        <EmptyDataTemplate>No Record Available</EmptyDataTemplate>  
                    </asp:GridView>  
    
    
    in CS Page
    
    gvEmployee.DataSource = dt;  
    gvEmployee.DataBind();  
    
    0 讨论(0)
  • 2020-11-28 12:08

    ASP.Net 4.0 added the boolean ShowHeaderWhenEmpty property.

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx


    <asp:GridView runat="server" ID="GridView1" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="First Name" DataField="FirstName" />
            <asp:BoundField HeaderText="Last Name" DataField="LastName" />
        </Columns>
    </asp:GridView>
    

    Note: the headers will not appear unless DataBind() is called with something other than null.

    GridView1.DataSource = New List(Of String)
    GridView1.DataBind()
    
    0 讨论(0)
  • 2020-11-28 12:08

    If you are working with ASP.NET 3.5 and lower, and your problem is relatively simple like mine, you can just return a null row from the SQL query.

    if not exists (select RepId, startdate,enddate from RepTable where RepID= 10)
         select null RepID,null StartDate,null EndDate
    else
         select RepId, startdate,enddate from RepTable where RepID= 10
    

    This solution does not require any C# code or ASP.NET code

    1. Make sure you cast the null columns into appropriate names, otherwise it will not work.
    2. Else block must be included which is the same query as in if not exists (query part)
    3. In my case if I am using @RepID instead of 10. Which is mapped to a DropDownList box outside gridview.

    Each time I change the drop down to select a different rep, Gridview is updated. If no record is found, it shows a null row.

    0 讨论(0)
  • 2020-11-28 12:09

    You can use HeaderTemplate property to setup the head programatically or use ListView instead if you are using .NET 3.5.

    Personally, I prefer ListView over GridView and DetailsView if possible, it gives you more control over your html.

    0 讨论(0)
提交回复
热议问题