B.Pumpkin
B.Pumpkin

Reputation: 201

Get value from selected row in WPF Datagrid

I have a DataGrid in my WPF projects

enter image description here

As you can see i'm able to select a row. I've made a double click method where i get the selected row. i want get just the ID part of that row.
This is how my method looks like

enter image description here

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    DataGridRow = sender as DataGridRow;
}

How to I just get the cell where I put the ID in it?

Many thanks in advance

Upvotes: 2

Views: 13144

Answers (3)

abdulmateenzwl
abdulmateenzwl

Reputation: 1

if you are using Data Binding from a Class object list you have to get the row like this

Your_Class row = SelectedStudents.SelectedItem as Your_Class;

Upvotes: 0

LuisEduardoSP
LuisEduardoSP

Reputation: 401

This is the code I used to get the ID of the double clicked row. I used an exploit of the double click (When you double click a row, you also select it). In my case, the column containing the id was the first (Row[0])

    private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataRowView dataRowView = (DataRowView)yourDataGridView.SelectedItem;
        int ID = Convert.ToInt32(dataRowView.Row[0]);
    }

Upvotes: 3

itzmebibin
itzmebibin

Reputation: 9439

If you are showing both XAML and cs code then only we can find the proper solution. Now I am assuming that you are displaying the contents by using binding from an observable collection of any class type. So you can easily get the ID field by,

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    ClassName classObj = dataGridName.SelectedItem as ClassName;
    string id = classObj.ID;
}

Upvotes: 3

Related Questions