Although GridView is a very powerful control provided by ASP.NET, it has a major flaw :
Whenever we are populating the GridView from database, then, if there is some data to be shown in the grid then it displays fine(along with Header and Footer) but if there is no data then nothing is shown in the grid even if the Header and Footer are not visible. However, in some cases we may need to display the Header and Footer even if there is no data in the GridView.
But there is a workaround for this :
When populating the data we have to check whether there is any data in the datasource or not. If there is no data in the datasource then we can add a blank record to the datasource. Then we have to bind the grid with the datasource. In this case even if there is no data to display, due to the blank row in the datasource, the GridView still has something to display.So the header and footer of the GridView appear properly.
Example:
private void PopulateGridView() { //Create the Connection, Command, DataAdapter object and the SQL query OleDbConnection oConnection = new OleDbConnection("Connection String is here"); String sqlSelectQuery = "SELECT PK_ID, EMP_NAME, EMP_AGE, EMP_SALARY FROM EMPLOYEE"; OleDbCommand oCommand = new OleDbCommand(oConnection, sqlSelectQuery); OleDbDataAdapter oAdapter = new OleDbDataAdapter(oCommand); //Create the DataSet object and fill it DataSet oDataSet = new DataSet(); oAdapter.Fill(oDataSet); //Check if the DataSet object is empty or not, //if empty then add a blank row. if(oDataSet.Tables[0].Rows.Count == 0) { oDataSet.Tables[0].Rows.Add(oDataSet.Tables[0].NewRow()); } //Populate the GridView gvMyGridView.DataSource = oDataSet; gvMyGridView.DataBind(); }
Hope this helps you while working with GridView.