Reputation: 20046
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
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
Reputation: 20870
You can use DataGrid.Items.Count
to get the number if items.
Upvotes: 42
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