locked
show header and footer of gridview even when gridview is empty and show no recordsfound message in empty gridview RRS feed

  • Question

  • User-2072603257 posted

    so basically i have a gridview with 2 columns (id, years). so i want that when data source for this gridview is empty, this gridview will show header and footer rows and inside gridview a message will be displayed to user (NO RECORDS FOUND). I have already tried using gridview properties ShowHeaderWhenEmpty="true" EmptyDataText="no records found", this method does show the message but as gridview is empty so it wont show the footer row. here is result

    then i read somethere that i should add an empty dummy row to gridview when gridview is empty and i also tried that, now it does show header and footer and also show the message but one samll problem is that the message is shown only in first column if gridvew and others columns are shown empty

    here is code behind used for above image output

    My code:

    <

      protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridview();
            }
        }
    
        public void BindGridview()
        {
            //if datatable has rows meaning data source is not empty 
            if(((DataTable)this.Get_Details()).Rows.Count > 0)
            {
                GridView1.DataSource = this.Get_Details();
                GridView1.DataBind();
            }
            else
            {
                //if the Data is empty then bind the GridView with an Empty Dataset
                GridView1.DataSource = this.Get_EmptyDataTable();
                GridView1.DataBind();
    
            }
        }
    
    
        public  DataTable Get_Details()
        {
            DataTable dt = new DataTable();
           
    
            string cs = WebConfigurationManager.ConnectionStrings["USTB-OBE-DATABASE"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                
                string quary = "select * from test3";
                SqlCommand cmd = new SqlCommand(quary, con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                con.Open();
                cmd.ExecuteNonQuery();
    
                dt = ds.Tables[0];
    
            }
    
            return dt;
        }
    
    
        public DataTable Get_EmptyDataTable()
        {
            DataTable datatableEmpty = new DataTable();
            
            //Here ensure that you have added all the column available in your gridview
            datatableEmpty.Columns.Add("id", typeof(string));
            
            datatableEmpty.Columns.Add("year", typeof(int));
            DataRow dataRow = datatableEmpty.NewRow();
           
            //Inserting a new row,datatable .newrow creates a blank row
            dataRow[0] = "no records found";
            datatableEmpty.Rows.Add(dataRow);//adding row to the datatable
            return datatableEmpty;
            
        }
    }

    >

    but i actually want that both columns are collspaned and whole row is shown as single coulumn and message is displayed inside meaning that i want this result

     

     but i also want the footer to be displayed also.

    i would really appreciate if anyone could help.

    Tuesday, June 23, 2020 5:04 PM

Answers

  • User1535942433 posted

    Hi dildarhussain997,

    Accroding to your description and codes,as far as I think,you could create a new blank cell and set the column span to the new added cell.

    More details,you could refer to below codes:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" ShowHeader="true">
                    <Columns>
                        <asp:BoundField  DataField="Id" HeaderText="Id" FooterText="Footer"/>
                        <asp:BoundField  DataField="Year" HeaderText="Year"  FooterText="Footer" />
                    </Columns>
                </asp:GridView>

    Code-behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Sum";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Sum");
                DataTable dt = new DataTable();
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                }
                else
                {
                    ShowNoResultFound(dt, GridView1);
                }
               
                conn.Close();
            }
            private void ShowNoResultFound(DataTable source, GridView gv)
            {
                source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
                                                  // Bind the DataTable which contain a blank row to the GridView
                gv.DataSource = source;
                gv.DataBind();
                // Get the total number of columns in the GridView to know what the Column Span should be
                int columnsCount = gv.Columns.Count;
                gv.Rows[0].Cells.Clear();// clear all the cells in the row
                gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
                gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
    
                //You can set the styles here
                gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
                gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
                gv.Rows[0].Cells[0].Font.Bold = true;
                //set No Results found to the new added cell
                gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 2:16 AM

All replies

  • User1535942433 posted

    Hi dildarhussain997,

    Accroding to your description and codes,as far as I think,you could create a new blank cell and set the column span to the new added cell.

    More details,you could refer to below codes:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" ShowHeader="true">
                    <Columns>
                        <asp:BoundField  DataField="Id" HeaderText="Id" FooterText="Footer"/>
                        <asp:BoundField  DataField="Year" HeaderText="Year"  FooterText="Footer" />
                    </Columns>
                </asp:GridView>

    Code-behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Sum";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Sum");
                DataTable dt = new DataTable();
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                }
                else
                {
                    ShowNoResultFound(dt, GridView1);
                }
               
                conn.Close();
            }
            private void ShowNoResultFound(DataTable source, GridView gv)
            {
                source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
                                                  // Bind the DataTable which contain a blank row to the GridView
                gv.DataSource = source;
                gv.DataBind();
                // Get the total number of columns in the GridView to know what the Column Span should be
                int columnsCount = gv.Columns.Count;
                gv.Rows[0].Cells.Clear();// clear all the cells in the row
                gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
                gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
    
                //You can set the styles here
                gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
                gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
                gv.Rows[0].Cells[0].Font.Bold = true;
                //set No Results found to the new added cell
                gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 2:16 AM
  • User-2072603257 posted

    THANK YOU yij sun

    it totally worked. really appreciated your help.

    Thursday, June 25, 2020 3:18 PM