【问题标题】:WPF DataGrid Get Cell Value Using ButtonWPF DataGrid 使用按钮获取单元格值
【发布时间】:2015-09-12 06:33:28
【问题描述】:

我有一个数据网格。当我单击删除按钮时,我想在我的数据网格中获取 ID 列值。我该怎么做?

这是我的数据网格 xaml。

<DataGrid x:Name="dtgridUser" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding ID}" Header="ID" />
        <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
        <DataGridTextColumn Binding="{Binding Age}" Header="Age" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button x:Name="btnDelete" Click="btnDelete_Click" >Delete</Button>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我通过将此代码添加到我的按钮来解决它。基本上,当我单击按钮时。它将获取单击按钮的行,然后从特定列中获取选择值。

    private void btnDelete_Click(object sender, RoutedEventArgs e)
    {
        DataGrid dataGrid = YOURDATAGRIDNAME;
        DataGridRow Row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
        DataGridCell RowAndColumn = (DataGridCell)dataGrid.Columns[0].GetCellContent(Row).Parent;
        string CellValue = ((TextBlock)RowAndColumn.Content).Text;
    
        MessageBox.Show(CellValue);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多