Hi, I have a gridview, which needs to show a total of distinct rows in a column. This can be shown in a label under the gridview.
Because there could be multiple same records with same product ids but with different other data, I need to show distinct count of products.
I have tried this way, but it shows only the count for the records in the current page. When I move to other page, the count reflects that particular page data.
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
var distinctRows = (from GridViewRow row in gdv.Rows
select row.Cells[1].Text
).Distinct().Count();
lblCount.Text = "Total number of records: " + distinctRows.ToString();
}
How do I show a distinct count of ALL the records in gridview irrespective of what page we are looking at?
In short, row.Cells[1] has product Ids which can contain duplicates and I want to extract distinct count of these product ids.
Member
48 Points
159 Posts
Distinct count of a column in gridview
Nov 03, 2016 02:47 PM|ASPbun|LINK
Hi, I have a gridview, which needs to show a total of distinct rows in a column. This can be shown in a label under the gridview.
Because there could be multiple same records with same product ids but with different other data, I need to show distinct count of products.
I have tried this way, but it shows only the count for the records in the current page. When I move to other page, the count reflects that particular page data.
How do I show a distinct count of ALL the records in gridview irrespective of what page we are looking at?
In short, row.Cells[1] has product Ids which can contain duplicates and I want to extract distinct count of these product ids.
And I've set the pagesize as 15.
Thanks for any help!
All-Star
18815 Points
3831 Posts
Re: Distinct count of a column in gridview
Nov 04, 2016 06:58 AM|Nan Yu|LINK
Hi ASPbun ,
You could disable paging temporarily and will re-bind the grid so that now we have access to all records . Code below is for your reference :
Code behind :
According to CustomerID , label shows "3" .
Best Regards,
Nan Yu