简体   繁体   中英

Get datagrid column value - WPF C#

I have a datagrid with two columns; well and workover. WPF:

<DataGrid  x:Name="PrintReport1" ItemsSource="{Binding TableResults}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="14" 
IsReadOnly="True" CanUserSortColumns="True" HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch"  SelectionMode="Extended" SelectionUnit="Cell">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Well" Binding="{Binding WellName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"  >

            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="dg_MouseLeftButtonDown_Injector" />
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Recommendated Workover Type" Binding="{Binding WorkoverRecommendation, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"  >                                                               </DataGridTextColumn>
   </DataGrid.Columns>

</DataGrid> 

There is a eventsetter event for the first column by which I can get the column value when user clicks on a particular cell as:

private void dg_MouseLeftButtonDown_Injector(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    dynamic dataObject = cell.DataContext;
    string InjectorName = dataObject.WellName;
    //do something...
}

My question is how can I get the corresponding second column value? For example, if the datagrid looks like this:

Well  Workover

Well1     ABC

Well2     DEF

and if user clicks on "Well2" cell, how do I get corresponding Workover value too; "DEF"?

dataObject.WorkoverRecommendation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM