Get selected row item in DataGrid WPF

Viewed 252725

I have a DataGrid, bound to Database table, I need to get the content of selected row in DataGrid, for example, I want to show in MessageBox content of selected row.

Example of DataGrid:

enter image description here

So, if I select the second row, my MessageBox has to show something like: 646 Jim Biology.

14 Answers

There are a lot of answers here that probably work in a specific context, but I was simply trying to get the text value of the first cell in a selected row. While the accepted answer here was the closest for me, it still required creating a type and casting the row into that type. I was looking for a simpler solution, and this is what I came up with:

MessageBox.Show(((DataRowView)DataGrid.SelectedItem).Row[0].ToString());

This gives me the first column in the selected row. Hopefully this helps someone else.

I got something like this:

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {

    DataGrid dg = (DataGrid)sender;

    DataRowView selectedRow = dg.SelectedItem as DataRowView;

    if(selectedRow != null) {
        txtXXX.Text = selectedRow["xxx"].ToString();
        txtYYY.Text = selectedRow["yyy"].ToString();
        txtZZZ.Text = selectedRow["zzz"].ToString();
    }

}

Your front code:

  • set your model namespace xmlns:viewModel="clr-namespace:CPL3_workstation.MVVM.ModelViews.Tables"
  • set xmlns:d="http://schemas.microsoft.com/expression/blend/2008", because d:DataContext="{d:DesignInstance...}
<DataGrid d:DataContext="{d:DesignInstance viewModel:TestViewModel, IsDesignTimeCreatable=True}"
          ItemsSource="{Binding Rows}"
          SelectedItem="{Binding Selector}">
    <DataGrid.Columns>
        <DataGridTextColumn
            Binding="{Binding Content1}"
            Header="Столбец 1" />
        <DataGridTextColumn
            Binding="{Binding Content2}"
            Header="Столбец 2" />
        <DataGridTextColumn
            Binding="{Binding Content3}"
            Header="Столбец 3" />
    </DataGrid.Columns>
</DataGrid>

Your ViewModel and the Model below:

namespace CPL3_workstation.MVVM.ModelViews.Tables
{
    public class TestViewModel
    {
        public IEnumerable<TestModel> Rows { get; set; }

        public TestModel Selector
        {
            get => selector;
            set => selector = value;
        }

        private TestModel selector;

        public TestViewModel()
        {
            Rows = new List<TestModel>
            {
                new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" },
                new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" },
                new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" },
                new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" }
            };
        }
    }

    public class TestModel
    {
        public string Content1 { get; set; }
        public string Content2 { get; set; }
        public string Content3 { get; set; }
    }
}

As you can see, the SelectedItem has a binding to the Selector property of the type of your model.

Related