Pritesh
Pritesh

Reputation: 3288

WPF Data Grid:- Iterate through Each row or record (C# 4.0)

Suppose I have select one row in Data Grid provided by VS2010.

Now suppose I need only records below the selected row then what should I have to do?

i think this could be done through iterating over each row of Data Grid.But how?

Why this? Because

Suppose I have bound one collection to datagrid.

And now I reorder the Data Grid using the header of columns of Data Grid.

Then records in Data Gridare reordered but the records in underling collection remains unordered.

Means re-ordering does not affect under lying collection.

So I can’t use it for getting the records below the selected row.

NOTE: here reordering is necessary

Thanks…

Upvotes: 0

Views: 1469

Answers (1)

brunnerh
brunnerh

Reputation: 184526

You can get the CollectionView which wraps the original collection and in which the ordering happens by using CollectionViewSource.GetDefaultView.

Your DataGrid needs to sync with the current item, then you get the position and get the object following that, e.g.:

<DataGrid IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Data}" />
var view = CollectionViewSource.GetDefaultView(Data) as ListCollectionView;
if (view != null)
{
    var i = view.CurrentPosition;
    var nextEmp = view.GetItemAt(i + 1) as Employee;
    if (nextEmp != null)
    {
        nextEmp.Name = "Steve!";
    }
}

Upvotes: 1

Related Questions