开发者

How to show No record found in GridView if binded datasource is List Collection

开发者 https://www.devze.com 2023-02-16 01:17 出处:网络
I have a gridview in which dtasource binded is a List which returns class type. If no records in list, I want to display \'No records found\' in GridView.

I have a gridview in which dtasource binded is a List which returns class type. If no records in list, I want to display 'No records found' in GridView.

List<Ticket> ticketList = new List&开发者_C百科lt;Ticket>();
ticketList = _tktBusiness.ReadAll(_tkt);
if (ticketList.Count > 0)
{
    gridTicketList.DataSource = ticketList;
    gridTicketList.DataBind();
}
else
{
}

In else part, what code i have to write to get desired output?Can anybody help?


You could use EmptyDataTemplate property of your grid. It gets or sets the user-defined content for the empty data row rendered when a GridView control is bound to a data source that does not contain any records. E.g.

<asp:gridview ...

    <emptydatatemplate>
        No Data Found.  
    </emptydatatemplate>

</asp:gridview>


You could set the Empty Data Text Property.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatext.aspx


In addition of using the EmptyDataTemplate, you could set your DataSource 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 could remove the if and bind it even if the ticketList is empty.

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

精彩评论

暂无评论...
验证码 换一张
取 消