Wednesday, September 18, 2013

How to get selectedItem value from a WPF DataGrid

For the DataGrid you can capture the SelectionChanged event.
In your XAML you would type SelectionChanged= and then press tab to allow it to create a method for you in the C# code.
Once you've done that you should go to the code files that accompanies your XAML file and find the event that was created.
For me it was:
SelectionChangedEvent
In this event I would add logic that would capture the msgId . Here is an example:
 private void SelectionChangedEvent(object sender, SelectionChangedEventArgs e)
        {
             //e.AddedItems contains the selected data
             System.Collections.IList selectedRow = e.AddedItems;
       //Logic to get the msgId from the IList
       // - in my example I am binding to a DataTable
       // - also notice that selectedRow[0] because I am
       // - allowing only 1 row selection at a time.
             if (selectedRow.Count > 0)      
                 msgId = ((AwareAlertUser)selectedRow[0]).MsgID.ToString();                   
        }

No comments:

Post a Comment