GridView - Show headers on empty data source

后端 未结 16 1093
挽巷
挽巷 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:14
        <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();  
    
    Help.. see that link:
    http://www.c-sharpcorner.com/UploadFile/d0e913/how-to-display-the-empty-gridview-in-case-of-no-records-in-d/
    
    0 讨论(0)
  • 2020-11-28 12:15

    After posting this I did come up with a way that works. However, I don't feel it is the best way to handle this. Any suggestions on a better one?

    //Check to see if we get rows back, if we do just bind.
    
    if (dtFunding.Rows.Count != 0)
    {
        grdFunding.DataSource = dtFunding;
        grdFunding.DataBind();
    }
    else
    {
      //Other wise add a emtpy "New Row" to the datatable and then hide it after binding.
    
         dtFunding.Rows.Add(dtFunding.NewRow());
         grdFunding.DataSource = dtFunding;
         grdFunding.DataBind();
         grdFunding.Rows[0].Visible = false;
    }
    
    0 讨论(0)
  • 2020-11-28 12:15

    I was using asp sqlDataSource. It worked for me when I set the CancelSelectOnNullParameter to false as below:

    <asp:SqlDataSource ID="SqlData1" runat="server" ConnectionString="" SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"> </asp:SqlDataSource>

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

    I was just working through this problem, and none of these solutions would work for me. I couldn't use the EmptyDataTemplate property because I was creating my GridView dynamically with custom fields which provide filters in the headers. I couldn't use the example almny posted because I'm using ObjectDataSources instead of DataSet or DataTable. However, I found this answer posted on another StackOverflow question, which links to this elegant solution that I was able to make work for my particular situation. It involves overriding the CreateChildControls method of the GridView to create the same header row that would have been created had there been real data. I thought it worth posting here, where it's likely to be found by other people in a similar fix.

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