X
X
xXNullXx2019-09-18 17:01:59
WPF
xXNullXx, 2019-09-18 17:01:59

What about type conversion?

Good day!
I need to know which row I selected on click in the DateGrid,

private void DGMaterials_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataRowView row = (DataRowView)dGMaterials.SelectedItem;

            MessageBox.Show(" ID: " + row["Id"]);
        }

But when I do this, I get an error on the 3rd line:
Could not cast object type 'DemoExam2018.ViewModel.OrderingMaterialsPageViewModel' to type 'System.Data.DataRowView'

Question and here conversion of type?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nujabes37, 2019-09-18
@xXNullXx

(DataRowView)dGMaterials.SelectedItem;
!!!
https://docs.microsoft.com/en-us/dotnet/csharp/pro...

A
Alexey Debelov, 2019-09-18
@AlienJust

Well, if you need it straight from the shoulder, then:

private void DGMaterials_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataGrid grid = sender as DataGrid;
    if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
    {
        DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
        DataRowView row = (DataRowView)dgr.Item;
        MessageBox.Show(" ID: " + row["Id"]);
    }
}

although Id probably gets from the ViewModel without any problems :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question