Joseph Reyes
Joseph Reyes

Reputation: 79

c# wpf datagrid clicked event and getting row value

How to do this? I want an Clicked-event-handler that triggers whenever I clicked a row. and also how can I get all the columns' values of the selected row. Thanks in advance. Any help will be much appreciated.

Upvotes: 1

Views: 3311

Answers (1)

AQuirky
AQuirky

Reputation: 5236

You want to implement an event handler for the selection changed event on the DataGrid

                <DataGrid x:Name="dataGridMaster" SelectionChanged="dataGridMaster_SelectionChanged">

As far as getting the column values of the selected row, a lot depends what you have bound the data grid to. For example if you have bound the DataGrid to a DataTable then here is how you would access the column values for the selected row in the event handler.

    private void dataGridMaster_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        foreach(DataRowView rv in e.AddedItems)
        {
            Debug.WriteLine("Row contents:");
            foreach (object d in rv.Row.ItemArray)
            {
                Debug.WriteLine("\t" + d.ToString());
            }
        }
    }

Upvotes: 1

Related Questions