如果搜索没有返回数据源中的记录,只是不确定语法,我想在屏幕上显示一条消息。
例如
if(gridview.datasource.[number of records] = 0)
{
do a thing
}
或者评估数据源后面的linq查询,
有什么想法吗?
谢谢
发布于 2011-05-20 21:05:21
您可以使用网格视图的行集合。
if(gridview.Rows.Count == 0)
{
do a thing
}
发布于 2011-05-20 21:22:09
您需要将数据源转换为它绑定到的正确类型。仅仅使用行并不总是给出数据源中的总计数。看看这个例子:
<asp:GridView ID="GridView1" runat="server"
AllowPaging="true" PageSize="3">
</asp:GridView>
在后面的代码中:
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
因此,您可以看到,由于启用了分页,因此仅计算行数仅返回'3‘。这是当前页的行数。但是,强制转换数据源将提供原始数据源返回的计数。因此,只要理解其中的区别,您就可以使用这两种方法中的一种。
https://stackoverflow.com/questions/6072218
复制相似问题