Reputation: 91
Afternoon everyone, I may be having a real dumb moment here but I have this section of code:
var playerCount = 0;
foreach (GridViewRow row in GridAttendees.Rows)
{
playerCount++;
}
LabelAttendees.Text = string.Format("Attending Players ({0})", playerCount);
It simply loops through a Gridview and counts the rows, updating the total. My first thought was to add it to the Page_Load event, however there are several functions on that page that can cause the Attendee List to increase.
My first thought was to add this snippet to all other events that change the attendee numbers, but we all know that we are duplicating code this way.
Instead I thought about adding it to my class, rather than the .aspx.cs page, as a method 'updateAttendeeCount()', and calling that instead.
How do I access 'GridRowView' in the class? And how do I access the GridView itself? By using the findcontrol?
Or is there another way to look at this (without having the page refresh/reload each time).
cheers guys Jason
Upvotes: 0
Views: 76
Reputation: 10218
If you want rows count then it could be done by SQL also
BY SQL:
SELECT COUNT(RecordID) AS getCount FROM TableName WHERE(FieldName=@FormFieldName)
or
By code: as mention in above answer
LabelAttendees.Text = "Attending Players "+ GridAttendees.Rows.Count.ToString();
Upvotes: 1
Reputation: 9862
why don't you use Count
Property:
int playerCount = GridAttendees.Rows.Count;
Upvotes: 3