locked
show header and footer when gridview null RRS feed

  • Question

  • User66371569 posted
    Hi

    I have a gridview and have select statment in page_load and i add in gridview footer some textboxes

    now gridview show when select return data

    i want to show header and footer when select return null

    I could show header by setting ShowHeaderWhenEmpty=true
    i want easy way to show footer too
    Tuesday, January 15, 2019 9:31 PM

All replies

  • User2103319870 posted

    i want to show header and footer when select return null

    GridView Header and Footer does not display when the GridView is empty (no rows). The work around is to ensure that a dummy row is returned from the database when there is no real data present.

    You can check the data from database is empty or not and then assign the data source accordingly.

    Please try with the sample Code:

    HTML Mark Up:

    <asp:GridView ID="GridView2" CellPadding="5" runat="server" ShowFooter="True"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
                    <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
                    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
                </Columns>
                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
               <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            </asp:GridView>

    Ensure that you have set the ShowFooter property of GridView to true.

    C#:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
    
            }
    
            //Function which will bind the details to GridView
            public void BindGridView()
            {
                //Checking if gridview datasource is empty or not
                if (((DataTable)this.Get_Details()).Rows.Count > 0)
                {
                    GridView2.DataSource = this.Get_Details();
                    GridView2.DataBind();
                }
                else
                {
                    //if the Data is empty then bind the GridView with an Empty Dataset
                    GridView2.DataSource = this.Get_EmptyDataTable();
                    GridView2.DataBind();
    
                } 
            }
            //Populate some dummy data for GridView 
            public DataTable Get_Details()
            {
                DataTable dt = new DataTable();
                DataRow dr;
                string Price = "15.123,25.123,35.3245345,45.567,55.345";
                string ContactName = "Name1,Name2,Name3,Name4,Name5";
                string City = "City1,City2,City3,City4,City5";
                string Country = "Country1,Country2,Country3,Country4,Country5";
    
                string[] list1 = ContactName.Split(',');
                string[] list2 = City.Split(',');
                string[] list3 = Country.Split(',');
                string[] list4 = Price.Split(',');
    
                dt.Columns.Add("Price", typeof(Double));
                dt.Columns.Add("City", typeof(string));
                dt.Columns.Add("Country", typeof(string));
                dt.Columns.Add("ContactName", typeof(string));
                dt.Columns.Add("Date", typeof(DateTime));
    
                //* Uncomment below section of code to see gridview populating with data
                //for (int i = 0; i < list1.Length; i++)
                //{
                //    dr = dt.NewRow();
                //    dr["ContactName"] = list1[i];
                //    dr["City"] = list2[i];
                //    dr["Country"] = list3[i];
                //    dr["Price"] = list4[i];
                //    dr["Date"] = DateTime.Now;
                //    dt.Rows.Add(dr);
                //}
    
                return dt;
            }
    
            //PopulateEmpty data for GridView 
            public DataTable Get_EmptyDataTable()
            {
                DataTable dtEmpty = new DataTable();
                //Here ensure that you have added all the column available in your gridview
                dtEmpty.Columns.Add("City", typeof(string));
                dtEmpty.Columns.Add("Country", typeof(string));
                dtEmpty.Columns.Add("ContactName", typeof(string));
                DataRow datatRow = dtEmpty.NewRow();
    
                //Inserting a new row,datatable .newrow creates a blank row
                dtEmpty.Rows.Add(datatRow);//adding row to the datatable
                return dtEmpty;
            }
    Tuesday, January 15, 2019 9:46 PM