KMC
KMC

Reputation: 20046

Row Count for WPF DataGrid?

How to count or return the last index of a DataGrid row? Apparently there's isn't a DataGrid.Count() property. I searched everywhere and I can't find an answer.

I could create a variable and use i++ as a counter for Row count, but doesn't a DataGrid has a built-in solution?

Upvotes: 22

Views: 45594

Answers (4)

RosnelOn
RosnelOn

Reputation: 71

Items.Count returns 1 when you Datagrid is editable. This don't work for me.

Try this:

int num=dataGrid.ItemsSource.OfType<object>().Count()

Upvotes: 1

RajnathKumar
RajnathKumar

Reputation: 37

store the count in a variable

 int num=dataGrid.Items.Count

Upvotes: 3

Greg Sansom
Greg Sansom

Reputation: 20870

You can use DataGrid.Items.Count to get the number if items.

Upvotes: 42

Youp Bernoulli
Youp Bernoulli

Reputation: 5654

As TBohnen.jnr mentioned you should count the items in the bounded datasource. You can retrieve the datasource by casting dataGrid.DataContext to the appropriate Type (List, IEnumerable, or any other Type) and call .Count on that object. It shouldn't be very hard...

Upvotes: 0

Related Questions