Reputation: 33
I have a datagrid with pair Name-Value. I want to find all rows where Name is equal to some string.
In WinForms, I could easily do it using "for" loop and get every Row's column[0] Value but in WPF I can't do like this...
DataGrid.Rows[i].Cells[0].Value
Then I just add to some list all 'i" values where Name is equal to needable and then remove all items from datagrid except them.
But how to iterate over all datagrid items/rows and get every column[0] value in WPF?
(I tried using foreach (DataGridRow in DataGrid.Items) but it not what I need...
Upvotes: 1
Views: 975
Reputation: 12119
In WPF you bind your DataGrid
ItemsSource
property to an ObservableCollection
(or CollectionViewSource
) and use LINQ to Objects to interrogate, filter or manipulate your underlying collection, or view(s) of that collection. You don't work with the DataGrid rows directly...
Example:
View:
<GridView ItemsSource="{Binding BoCollection, Mode=TwoWay}"/>
ViewModel (implementing INotifyPropertyChanged):
private ObservableCollection<BO.MyObject> boCollection;
public ObservableCollection<BO.MyObject> BoCollection
{
get { return boCollection; }
set
{
folderEmails = value;
NotifyPropertyChanged(m => m.BoCollection);
}
}
Upvotes: 1