How to get the value of selected cells in WPF DataGrid using selectionchanged event?

Scenario: Read the cell value from the selected row in a WPF DataGrid and then store it into a variable using C#.
(Refer the below screen shot)










Above screen shot is a WPF DataGrid, Let's assume that we want to read the file name from the selected row(first row).

Resolution: We cannot apply the same logic in WPF DataGrid for read as Asp.NET gridview because it contains different approach of implementation. Let's assume that we want to get the cell value from selected row in WPF DataGrid using SelectionChanged event, use the below line of code-


string  ID = (DataGid.SelectedCells[0].Column.GetCellContent(selected) as TextBlock).Text;

Above line will provide the value from first cell of the selected row. 

Value of the ID variable will be r1.

I hope this article will help you.

Post a Comment

1 Comments