How can I get the selected value of a WPF DataGrid cell?

In my C # wpf application, I took a DataGrid, which is limited to data from the database. All values ​​are displayed in this DataGrid. But I want to get the selected cell value.

Here is my code that is limited by datatable:

dataGrid1.ItemsSource = datatable1.DefaultView; 

Please give me a solution to find the value of the cell. I found the selected index with the following code:

dataGrid1.SelectedIndex
+3
source share
2 answers

Assuming you are editing a cell DataGridTextColumn...

Use ewhich is in the event dataGrid1_CellEditEnding, for example:

((TextBox)e.EditingElement).Text

This will give you typed text.

+1
source

basically you can do this:

        var cellInfos = dataGrid1.SelectedCells;

        foreach (DataGridCellInfo cellInfo in cellInfos)
        {
            if (cellInfo.IsValid)
            {
                // element will be your DataGridCell Content
                var element = cellInfo.Column.GetCellContent(cellInfo.Item); 

                if (element != null)
                {
                    var myCell = element.Parent as DataGridCell;
                }
            }
        }
0
source

Source: https://habr.com/ru/post/1783089/


All Articles