0
votes

I have a DataGrid in xaml and I populate it by setting the ItemsSource like this

var myList = new List<Product>();
//Product is a standard class created for the purpose of populating the datagrid

 myList.Add(new Product(){Name = "bla", SerialNumber = 1234})
 myList.Add(new Product(){Name = "somename", SerialNumber = 567})

dataGrid.ItemsSource = myList;

So basically each row in the datagrid is populated with values from an instance of the Product class.

Later in the code when the user selects a row in the datagrid and presses a key, I get the selected row like this

dataGrid.SelectedItem

But now, I cant seem to figure out how to get the Product instance which was used to create that selected row.

So for example something like var test = dataGrid.SelectedItem.Instance which would be the same as doing this this var test = new Product(){Name = "bla", SerialNumber = 1234}

1

1 Answers

0
votes

The dataGrid.SelectedItem property should return your Product instance, but if you do have a DataGridRow object, then you can get the related data item using the DataGridRow.Item Property:

 Product selectedProduct = (Product)yourDataGridRow.Item;