Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get row in datagrid

Tags:

c#

wpf

row

datagrid

I tried to get row like this :

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;

But I only got null. Is there another solution? What am I doing wrong?

I want to get data from my cells. My cells are checkboxes.

like image 922
kartal Avatar asked Nov 25 '22 06:11

kartal


1 Answers

This depends on how / when you are attempting to get this data. WPF is more geared towards accessing the data by the objects bound in the ItemsSource. So, if your ItemsSource is a List of MyObject, then the specific row will be of type MyObject instead of a pure DataRow.

If you are accessing the data by means of clicking on it, you can do something like this:

var currentItem = myDataGrid.SelectedItem as MyObject;

Now, you have the current MyObject in it's originally intended form rather than picking at the grid.

like image 145
Xcalibur37 Avatar answered Dec 10 '22 15:12

Xcalibur37