GridView - Show headers on empty data source

后端 未结 16 1092
挽巷
挽巷 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:10

    Juste add ShowHeaderWhenEmpty property and set it at true

    This solution works for me

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

    You can set the ShowHeadersWhenNoRecords property of the ownertableview to true. aspx:

    <asp:GridView ID="RadGrid2" runat="server" >       
    <MasterTableView ShowHeadersWhenNoRecords="true"  > 
    

    Also when the datasource for the GridView is null(when no records), you can try setting it as shown below: c#:

      if (GridView1.DataSource == null)  
      {  
            GridView1.DataSource = new string[] { };  
      } 
      GridView1.DataBind();
    
    0 讨论(0)
  • 2020-11-28 12:12

    set "<asp:GridView AutoGenerateColumns="false" ShowHeaderWhenEmpty="true""

    showheaderwhenEmpty Property

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

    Use an EmptyDataTemplate like below. When your DataSource has no records, you will see your grid with headers, and the literal text or HTML that is inside the EmptyDataTemplate tags.

    <asp:GridView ID="gvResults" AutoGenerateColumns="False" HeaderStyle-CssClass="tableheader" runat="server">
        <EmptyDataTemplate>
            <asp:Label ID="lblEmptySearch" runat="server">No Results Found</asp:Label>
        </EmptyDataTemplate>
        <Columns>
            <asp:BoundField DataField="ItemId" HeaderText="ID" />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            ...
        </Columns>
    </asp:GridView>
    
    0 讨论(0)
  • 2020-11-28 12:14

    I found a very simple solution to the problem. I simply created two GridViews. The first GridView called a DataSource with a query that was designed to return no rows. It simply contained the following:

        <Columns>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left">
                <HeaderTemplate>
    
                   <asp:Label ID="lbl0" etc.>  </asp:Label>
                   <asp:Label ID="lbl1" etc.>  </asp:Label>
    
                </HeaderTemplate>
            </asp:TemplateField>
        </Columns>
    

    Then I created a div with the following characteristics and I place a GridView inside of it with ShowHeader="false" so that the top row is the same size as all the other rows.

    <div style="overflow: auto; height: 29.5em; width: 100%">
        <asp:GridView ID="Rollup" runat="server" ShowHeader="false" DataSourceID="ObjectDataSource">
            <Columns>
                <asp:TemplateField HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
    
                   <asp:Label ID="lbl0" etc.>  </asp:Label>
                   <asp:Label ID="lbl1" etc.>  </asp:Label>
    
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    
    0 讨论(0)
  • 2020-11-28 12:14
    <asp:GridView ID="grdGroup"  EmptyDataText="No Records Found" ShowHeaderWhenEmpty="True" runat="server">
    

    This is a basic example of Gridview with EmptyDataText and ShowHeaderWhenEmpty

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