DataGrid for DataRowView

I need to charge the value of the element from the dataGrid when clicked by the user, in datarowview, to take the first value of "IdEmployee" and assign it to a variable.

This is my method, the problem in my dataRowView variable is Null!

How can i fix this?

private void _employeedataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataRowView dataRowView = _employeedataGrid.CurrentCell.Item as DataRowView;
    var idEmployee = Convert.ToInt32(dataRowView.Row[0]);

    .......
} 
+3
source share
3 answers

This is because it _employeedataGrid.CurrentCell.Itemcannot be distinguished as DataRowView. Why don't you try CurrentRowinstead CurrentCell?

private void _employeedataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataRowView dataRowView = _employeedataGrid.CurrentRow.Item as DataRowView;
    var idEmployee = Convert.ToInt32(dataRowView.Row[0]);
    .......
} 
+2
source

What you need

DataRowView view = _employeedataGrid.Items[_employeedataGrid.SelectedIndex] as DataRowView;
+1
source
            //during datagrid events like RowEditEnding, SelectionChange event 'e' will be very useful as...
            DataGridRow row1 = e.Row;
            int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);

            //or try this if works
            int i_row = e.Row.GetIndex();
0
source

All Articles