banner



How To Clear Gridview In Asp Net

  • Updated date Mar 24, 2015
  • 264.1k
  • 3

In this article you will learn to display an empty GridView when there are no records in the database.

Scenario

Consider a table named Employees with the columns Id, Name, Designation and Salary and you have been asked to display the data on a web page.

The requirement is quite simple and can be done using a Grid View. Now consider a case when your query fetches no data and there is nothing to display in the Grid View. In that case, we need to display a custom message or an alert to the user that there is no data available. There are several ways to do that but I will show you a simple method to do it.

Suppose we need to find the list of Employees with Salary < 10,000. If you look at the preceding table, it is clear that no employee have a Salary less than 10,000. So our query fetches nothing for the Grid View.

In that case, we need to show the empty Grid View with a message that the records are not available. To do that, we need to enable the Boolean property ShowHeaderWhenEmpty to True. Be sure you're using the ASP.NET 4.0 or later version to use this property.

Also, we need to add the <EmptyDataTemplate></EmptyDataTemplate> property inside the grid view to display the message.

Code

The following is the EmployeeDetails.aspx page:

  1. < %@ Page Language = "C#" AutoEventWireup = "true" CodeBehind = "EmployeeDetails.aspx.cs" Inherits = "GridView.EmployeeDetails"  % >
  2. <!DOCTYPE html>
  3. < html
  4. xmlns = "http://www.w3.org/1999/xhtml" >
  5. < head runat = "server" >
  6. < title > Employee Details </ title >
  7. </ head >
  8. < body >
  9. < form id = "form1" runat = "server" >
  10. < div >
  11. < h3 > Employee Details </ h3 >
  12. < asp:GridView ID = "gvEmployee" runat = "server"
  13. AutoGenerateColumns = "False" ShowHeaderWhenEmpty ="True" >
  14. < Columns >
  15. < asp:BoundField DataField = "Id" HeaderText = "Id" />
  16. < asp:BoundField DataField = "Name" HeaderText = "Name" />
  17. < asp:BoundField DataField = "Designation" HeaderText = "Designation" />
  18. < asp:BoundField DataField = "Salary" HeaderText = "Salary" />
  19. </ Columns >
  20. < EmptyDataTemplate > No Record Available </ EmptyDataTemplate >
  21. </ asp:GridView >
  22. </ div >
  23. </ form >
  24. </ body >
  25. </ html >

In theEmployeeDetails.aspx.cs page, we need to add a fix to check the condition if the Grid View is empty or not. I'm going to assign an empty DataTable to the grid view in case no records are fetched.

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Configuration;
  4. using  System.Data.SqlClient;
  5. using  System.Linq;
  6. using  System.Web;
  7. using  System.Web.UI;
  8. using  System.Web.UI.WebControls;
  9. namespace  GridView
  10. {
  11. public  partial class  EmployeeDetails: System.Web.UI.Page
  12.     {
  13. protected void  Page_Load( object  sender, EventArgs e)
  14.         {
  15. string  connect = ConfigurationManager.ConnectionStrings[ "SQLConnect" ].ConnectionString;
  16. using (SqlConnection con = new  SqlConnection(connect))
  17.             {
  18.                 var query ="SELECT * from Employees where Salary < 10000" ;
  19.                 SqlCommand cmd =new  SqlCommand(query, con);
  20.                 con.Open();
  21.                 SqlDataReader dr = cmd.ExecuteReader();
  22. if  (dr.hasRows)
  23.                 {
  24.                     gvEmployee.DataSource = dr;
  25.                     gvEmployee.DataBind();
  26.                 }
  27. else
  28.                 {
  29.                     DataTable dt =new  Datatable();
  30.                     gvEmployee.DataSource = dt;
  31.                     gvEmployee.DataBind();
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }

It should be noted that the ADO.Net code present in this article is not optimized and is used only for the demo. In real projects, Stored Procedures and other conventions should be used for the query. Now run the application and the output is as follows:

Conclusion

Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the universe!

How To Clear Gridview In Asp Net

Source: https://www.c-sharpcorner.com/UploadFile/d0e913/how-to-display-the-empty-gridview-in-case-of-no-records-in-d/

Posted by: penaknotans.blogspot.com

0 Response to "How To Clear Gridview In Asp Net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel