How to show No entry found in GridView if linked data source - List List


I have a gridview in which dtasource binded is a List that returns the type of the class. If there are no entries in the list, I want to display "No Entries" in the GridView.

List<Ticket> ticketList = new List<Ticket>(); ticketList = _tktBusiness.ReadAll(_tkt); if (ticketList.Count > 0) { gridTicketList.DataSource = ticketList; gridTicketList.DataBind(); } else { } 

On the other hand, what code do I need to write to get the desired result? Can anyone help?

+4
source share
3 answers

You can use the EmptyDataTemplate property of your grid. It receives or sets user-defined content for the empty data row displayed when the GridView control is bound to a data source that does not contain records. For instance.

 <asp:gridview ... <emptydatatemplate> No Data Found. </emptydatatemplate> </asp:gridview> 
+8
source

In addition to using EmptyDataTemplate, you can set your data source to null.

 List<Ticket> ticketList = new List<Ticket>(); ticketList = _tktBusiness.ReadAll(_tkt); if (ticketList.Count > 0) { gridTicketList.DataSource = ticketList; } else { gridTicketList.DataSource = null; } gridTicketList.DataBind(); 

Or you can remove the if and bind it even if listList is empty.

 List<Ticket> ticketList = new List<Ticket>(); ticketList = _tktBusiness.ReadAll(_tkt); gridTicketList.DataSource = ticketList; gridTicketList.DataBind(); 
0
source

Source: https://habr.com/ru/post/1343315/


All Articles