开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜