Michael A
Michael A

Reputation: 9930

How do I get the value of one row from a selected item in a datagrid?

I have a WPF datagrid and I'm trying to get the value of my first row for the seleted item. I've tried using the following based on previous questions I've found here, but had no success:

var eventid = dataGridArchiveQueue.SelectedItem;

also tried:

var eventid = dataGridArchiveQueue.Columns[0].GetValue(dataGridArchiveQueue.SelectedItem);

What am I not understanding?

Upvotes: 0

Views: 102

Answers (2)

Stafford Williams
Stafford Williams

Reputation: 9806

If you are using MVVM, you can bind an object in your view model to the datagrid's SelectedItem property as such;

<DataGrid SelectedItem="{Binding SelectedItemInMyViewModel}" ... >
...
</DataGrid>

Then in your view model, you can expose this property with whatever type you like (meaning you won't need to cast it, but providing it is the type as the Array/List objects you are binding to the DataGrid's ItemsSource

For example if you were binding a List to the datagrid, on the view model you could have;

public Person SelectedItemInMyViewModel { get; set; }

You can then look at that property whenever you like to see the selected item.

Upvotes: 1

brunnerh
brunnerh

Reputation: 185578

SelectedItem is your row boxed to object, you need to cast it back to the item type to access its members.

Upvotes: 0

Related Questions