Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to get row count in Datagrid
4 replies. Latest Post by bartczernicki on November 20, 2008.
(0)
raj.jadhav
Member
54 points
107 Posts
11-20-2008 12:49 AM |
Hi,
In Silverlight 2 how can I count rows in datagrid using for loop.
Thanks,
Rajesh
bartczer...
Contributor
4162 points
730 Posts
11-20-2008 1:24 AM |
You don't want to iterate over the datagrid, you want to iterate over the binding source. The ItemSource property on the grid is an IEnumerable object and you can return an enumerator.
For example, say we had a grid that is bound to three strings. here is how you could loop through the bound items and get the count...
{
count++;
}
You could also access each object inside the foreach if you wanted to.
amyo
3620 points
495 Posts
if( youDataGrid.ItemsSource!=null)
var count = youDataGrid.ItemsSource.OfType<object>().Count();
11-20-2008 5:45 AM |
If your only target is to count number of rows in a DataGrid then use my solution that is easier.
11-20-2008 10:56 AM |
The question was asked how to get the count using a for loop. That was the answer provided. You are probably right that its easier, but you have no idea why the person is asking the question...so the best is to answer the question exactly.