Reputation: 577
Scenario is that i want to get vales from the selected row onMouseDoubleClick event. I cast the row to DataRowView but the object get no value. At the time of debugging when i hover my cursor on SelectedItem it show me the values but it does not cast it to DataRowView. The gridview is populated using linq from cs code. no datasource is used.
.cs code:
private void _gvDoctorVisit_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView row = _gvDoctorVisit.SelectedItem as DataRowView;
int visitid = Convert.ToInt32(row.Row["visit_id"]);
MessageBox.Show(visitid.ToString());
}
.xaml Code:
<DataGrid x:Name="_gvDoctorVisit" AutoGenerateColumns="False" EnableRowVirtualization="True" Margin="10,118,10,10" RowDetailsVisibilityMode="VisibleWhenSelected" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" HorizontalScrollBarVisibility="Visible" MouseDoubleClick="_gvDoctorVisit_MouseDoubleClick" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="visit_idColumn" Binding="{Binding visit_id}" Header="Visit ID" IsReadOnly="True" Width="100"/>
<DataGridTextColumn x:Name="person_idColumn" Binding="{Binding Patient_name}" Header="Patient Name" IsReadOnly="True" Width="200"/>
<DataGridTextColumn x:Name="visit_timeColumn" Binding="{Binding visit_time}" Header="Visit Time" IsReadOnly="True" Width="140" />
<DataGridTextColumn x:Name="visit_typeColumn" Binding="{Binding visit_type}" Header="Visit Type" IsReadOnly="True" Width="733"/>
</DataGrid.Columns>
</DataGrid>
Upvotes: 2
Views: 1332
Reputation: 1523
Do not use DataRowView ... that is not relevant for WPF's DataGrid. You must be thinking of a different framework.
If you are populating the data on your DataGrid simply by setting the ItemsSource property to a collection of objects that you got from your Linq code, then the type of SelectedItem will simply be the same object type that you passed in. So, you should be able to just do the following. (Feel free to replace the "dynamic" with whatever your actual class is, if you have one. Otherwise, you can just leave it as is.)
private void _gvDoctorVisit_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
int visitid = ((dynamic)_gvDoctorVisit.SelectedItem).visit_id;
MessageBox.Show(visitid.ToString());
}
Upvotes: 3