Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get cellvalue in datagrid, on mouse double click event in WPF

I am new in wpf.

I am binding below datagrid in wpf

<DataGrid AutoGenerateColumns="True" 
          Loaded="dataGrid1_Loaded" 
          MouseDoubleClick="dataGrid1_MouseDoubleClick" 
          Height="350" 
          Width="1200"
          Name="dataGrid1" />

I have one "OID" name column in the grid, and I want to get the value of this column when user double click on row. How can I get it ? which event I should use for it ?

I can use view button in datagrid to get column value, but I don't know how to bind buttons/link in datagrid and how to handle it for get column value ?

Thanks C.P

like image 561
Chhatrapati Sharma Avatar asked Dec 02 '25 03:12

Chhatrapati Sharma


1 Answers

First of all, if you are about to develop a pretty big (over 1 week of development) application you really should start thinking about MVVM.

In this architecture you wont use event handlers in the code behind. instead, you will use Commands to send info from your UI to your logic, and Bindings from logic to the UI.

But to answer your current question, you can use the MouseDoubleClick event and retrieve your data from there

    private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var grid = sender as DataGrid;

        var cellValue = grid.SelectedValue;
    }

This will get the value when the user double clicks the row in the specific column. If you want to get the value of the cell no matter where the user double clicked the row, use:

grid.SelectedItem

to get the object that the row represents, and from there retreive your property your column is binded to.

Hope this helps

like image 121
Omri Btian Avatar answered Dec 04 '25 17:12

Omri Btian