简体   繁体   English

获取datagrid列值-WPF C#

[英]Get datagrid column value - WPF C#

I have a datagrid with two columns; 我有一个包含两列的数据网格; well and workover. 好,修井。 WPF: 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: 第一列有一个事件eventsetter事件,当用户单击特定单元格时,通过该事件可以获取列值:

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: 例如,如果datagrid如下所示:

Well  Workover

Well1     ABC

Well2     DEF

and if user clicks on "Well2" cell, how do I get corresponding Workover value too; 如果用户单击“ Well2”单元,我也如何获得相应的修井值; "DEF"? “ DEF”?

dataObject.WorkoverRecommendation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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