Friday, March 23, 2012

Number of records in SQLDataSource/GridView

What is the easiest way to obtain number of records in SQLDataSource (using select statement)/GridView. All that I've found in forums seems to be very difficult for such trivial task... Thank you!

I've found 2 GridView properties: pagesize and pagecount. So I can write:

Label1.Text =(GridView1.PageSize*GridView1.PageCount).ToString + " records found."

But the last page of the GridView may contain less than PageSize number. Is it possible to count records number on the last page of the GridView?

|||Have you tried GridView.Rows.Count?|||Yes, I've tried GridView.Rows.Count but it counts records only in current GridView page...|||

Yes you're right. Then the only way I can figure out is to retrieve the row count from SqlDataSource:

DataSourceSelectArguments dssa = new DataSourceSelectArguments();

dssa.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
dssa.RetrieveTotalRowCount = true;

DataView dv = (DataView)SqlDataSource1.Select(dssa);

Response.Write(dv.Table.Rows.Count);

But the dv.Table.Rows.Count will always return 0 if the SqlDataSource has some parameter bound to a control, so confused.

No comments:

Post a Comment