Click here to Skip to main content
15,885,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have gridview which takes datatable from sql server. When the datatable has no rows i have written a funtion to bind the gridview from a seperate source which adding a new row to the gridview also merging all columns and displaying error messeage"No Result found" but this is not working the all columns are displaying as it is. The function for binding empty grid is given below

C#
private void ShowNoResultFound(DataTable source, GridView gv, bool footerStatus)
       {
           source.Rows.Add(source.NewRow());
           gv.DataSource = source;

           gv.ShowFooter = footerStatus;

           int columnsCount = gv.Columns.Count;
           gv.DataBind();
           gv.Rows[0].Cells.Clear();
           gv.Rows[0].Cells.Add(new TableCell());
           gv.Rows[0].Cells[0].ColumnSpan = columnsCount;


           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;

           gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";

       }
Posted
Updated 26-Jun-13 1:28am
v2
Comments
Orcun Iyigun 26-Jun-13 7:33am    
Why do you need to make it complicated? Alerting the user with a client-sided message is not enough?
Rinshad Hameed 26-Jun-13 7:37am    
I have a footer row for this grid which is used to add data. And there is a button to show footer . even if the grid is empty the footer should be displayed there is no way other than this to achieve that as per my knowledge. please suggest some other methods if you know.
Rinshad Hameed 26-Jun-13 7:41am    
Its only an adjustment to show footer. Otherwise there will be no grid displayed

1 solution

Please use grid view RowDataBound event. I just tested. You can refer below code but you need to use some flag in RowDataBound so that it get executed only when you have empty data.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {

          if (e.Row.RowType == DataControlRowType.DataRow)
          {   var columnsCount = e.Row.Cells.Count;
              e.Row.Cells.Clear();
              var tc = new TableCell();
              tc.Text = "Empty";
              tc.ColumnSpan = columnsCount;
              e.Row.Cells.Add(tc);
          }
      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900