开发者

count records in datasource - c#

I want to show a message on the screen if a search returns no records in a datasource, just not sure of the syntax,

eg

if(gridview.datasource.[number开发者_运维百科 of records] = 0)
{
 do a thing
}

or evaluate the linq query behind the datasource,

any ideas?

thanks


You can use the Rows collection of the gridview.

if(gridview.Rows.Count == 0)
{
 do a thing
}


You need to cast your datasource to the correct type that it is bound to. Just using rows will not always give you the total count in the datasource. Look at this example:

<asp:GridView ID="GridView1" runat="server" 
    AllowPaging="true" PageSize="3">
</asp:GridView>

And in code behind:

var fruit = new List<string>() 
    { "banana", "orange", "apple", "strawberry", "melon", "grape" }

GridView1.DataSource = fruit;
GridView1.DataBind();

int rowsCount = GridView1.Rows.Count; // rowsCount = 3

int dataCount = ((List<string>)GridView1.DataSource).Count; // dataCount = 6

So you can see that just counting the rows only returns '3', since paging is enabled. This is the count of the current page of rows. However, casting the datasource gives you the count returned by the original datasource. So you can use either so long as you understand the difference.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜