0
votes

I use WPF Metro datagrid. When I use DataGridRow for Getting data from selected row content, Always Null data is returned at var 'rowview'. What is my problem?

This is my code.

    private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        IInputElement element = e.MouseDevice.DirectlyOver;
        if (element != null && element is FrameworkElement)
        {
            if (((FrameworkElement)element).Parent is DataGridCell)
            {
                DataGrid grid = sender as DataGrid;
                if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                {
                    DataGridRow rowview = grid.SelectedItem as DataGridRow;
                    if (rowview != null)
                    {
                        ProjectInfo addJobWindow = new ProjectInfo();
                        addJobWindow.ShowDialog();
                    }
                }
            }
        }
    }


    <DataGrid x:Name="DgProjectInfo" ItemsSource="{Binding Projects}" 

AutoGenerateColumns="False" Margin="-11,11,211,-1" MouseDoubleClick="DataGrid_MouseDoubleClick" IsReadOnly="True">

2
u sure u wanna check rowView != null ? you are not using rowView anyway ? just check is grid.selectedItem != null.... rest read ans belowMuds

2 Answers

2
votes

You cannot convert grid.SelectedItem as DataGridRow. Refer the below code to get the datagrid row.

DataGridRow rowview = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(grid.SelectedIndex);
0
votes

Maybe grid.SelectedItem isn't a DataGridRow? You can check by mousing over the property "SelectedItem" and reading the prompt.

You could also type:

var temp = grid.SelectedItem;

and add a breakpoint after this line to easily determine the type.