3

My WPF DataGrid looks like this enter image description here

The markup

    <DataGrid x:Name="Processes" AutoGenerateColumns="False" ItemsSource="{Binding Path=ProcessesBinding}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="807" Margin="13,32,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Id}" Header="ID" Width="50" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Friendlyname}" Header="Name" Width="200" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Process}" Header="Process" Width="180" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Status}" Header="Status" Width="180" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Autostart}" Header="Auto Start" Width="100" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Autorestart}" Header="Auto Restart" Width="100" CanUserResize="False" IsReadOnly="True"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

The way I fill the data in

foreach (var ... in ...)
{
    dataTable.Rows.Add(...);
}

And how it's applied

Processes.ItemsSource = dataTable.DefaultView;

I already searched the web and found this, however, I couldn't deduce anything to what I need.

What I want: No matter where I click on row 1 (ID 13 above), an event is run. The only thing I do need is the ID of the row which was clicked so I can handle the 'request' further.

That's what I vaguely got in my mind

private void CellClick(int ID) { // show new window with id
}

3 Answers 3

2

There is an event of the datagrid named as MouseDoubleClick. You can use it like this to get the whole object of the row that is double clicked.

private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                DataGrid grid = sender as DataGrid;
                if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                {
                    DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
                    YourClass obj = dgr.Item as YourClass;
                    this.selectedIndex = grid.SelectedIndex;
                    int id = obj.ID;
                }
            }
        }

As I don't know the name of your class, so you can replace YourClass with the name of your class.

Basically what this code does is it detects the double click event on Datagrid. Then it checks the event is performed on DataGridRow. And then it type casts the Item of that row to your defined class. And all of the information of that row will be populated in the object you are receiving the type cast object in.

Sign up to request clarification or add additional context in comments.

Comments

1

If you dont go the MVVM approach but simply with EventHandlers, than you can add a handler for change of selected cells:

<DataGrid SelectedCellsChanged="yourGrid_SelectedCellsChanged" ...>

and in the code-behind file

 private void yourGrid_SelectedCellsChanged(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
    {
        // retrieve the id here, like in the answer above
    }

Comments

0

In addition to what @UmairFarooq suggested, if you have a control, say, a button in a cell, an alternate way of doing it can be as follows:

Basically your control in the cell will inherit the DataContext of a row data object. Let's call it as MyObject. So, MyObject.ID is what you want.

private void Button_Click(object sender, MouseButtonEventArgs e)
{
     MyObject obj = ((FrameworkElement)sender).DataContext as MyObject;
    //Now you can do whatever you wanted to do with MyObject.ID
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.