Refat Sardar
Refat Sardar

Reputation: 67

How can I get WPF DataGrid Selected Cell Value?

In my C# wpf application i have taken a DataGrid which is bounded by a datatable from database. All values are displayed in this DataGrid. But i want to get the selected cell value.

Here is my code which is bounded by datatable:

dataGrid1.ItemsSource = datatable1.DefaultView; 

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

dataGrid1.SelectedIndex

Upvotes: 3

Views: 4260

Answers (2)

WiiMaxx
WiiMaxx

Reputation: 5420

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;
                }
            }
        }

Upvotes: 0

user980150
user980150

Reputation: 186

Assuming that you are editing a DataGridTextColumn cell...

Use e which is in the dataGrid1_CellEditEndingEvent like so:

((TextBox)e.EditingElement).Text

This will give you the typed text.

Upvotes: 1

Related Questions