Reputation: 197
I have DataGridRow
of a DataGrid
, how can I get the individual column values from it? See the code below.
var itemsSource = MESearchDatagrid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
var row = MESearchDatagrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
//How to get the Cell value?
}
}
}
Upvotes: 5
Views: 3749
Reputation: 169150
There is no reason to call the ContainerFromItem
method here. The ItemsSource
contains the actual data objects. You only need to cast them to your type (called YourClass
in the following sample code):
var itemsSource = MESearchDatagrid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource.OfType<YourClass>())
{
var prop = item.Property1;
//...
}
}
Property1
is a property of YourClass
.
Upvotes: 2
Reputation: 457
You can cast your row.item to your initial class type.
var itemsSource = MESearchDatagrid.ItemsSource;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
var row = MESearchDatagrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
var data = row.Item as UserClass;
MessageBox.Show(data.Name);
}
}
}
Upvotes: 2