Counting records in a data source - C #

I want to show a message on the screen if the search does not return any records in the data source, just not sure of the syntax,

eg,

if(gridview.datasource.[number of records] = 0)
{
 do a thing
}

or evaluate the linq query for the data source,

any ideas?

thank

+3
source share
2 answers

You can use the Rows gridview collection.

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

You need to point your data source to the correct type to which it is bound. Just using strings will not always give you the total in the data source. Take a look at this example:

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

And in the 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

, , "3", . . , , . , .

+3

All Articles