Skip to main content

Microsoft Silverlight

Answered Question How to get row count in DatagridRSS Feed

(0)

raj.jadhav
raj.jadhav

Member

Member

54 points

107 Posts

How to get row count in Datagrid

Hi,

  In Silverlight 2 how can I count rows in datagrid using for loop.

 

Thanks,

Rajesh

bartczernicki
bartczer...

Contributor

Contributor

4162 points

730 Posts

Answered Question

Re: How to get row count in Datagrid

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...

dGrid.ItemsSource = new List<string>{"test", "test2", "test3"};

int count = 0;

// use the enumerator from the Grid

foreach (string c in dGrid.ItemsSource)

{

count++;

}

int finalcount = count;

You could also access each object inside the foreach if you wanted to.

amyo
amyo

Contributor

Contributor

3620 points

495 Posts

Re: How to get row count in Datagrid

if( youDataGrid.ItemsSource!=null)

var count = youDataGrid.ItemsSource.OfType<object>().Count();

Amyo Kabir
Solution Architect & Sr. Developer
Blog

amyo
amyo

Contributor

Contributor

3620 points

495 Posts

Re: How to get row count in Datagrid

If your only target is to count number of rows in a DataGrid then use my solution that is easier.

Amyo Kabir
Solution Architect & Sr. Developer
Blog

bartczernicki
bartczer...

Contributor

Contributor

4162 points

730 Posts

Re: How to get row count in Datagrid

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities