首页 > 解决方案 > 如何在我的 DataGrid 中获取选定单元格的当前列(C# WPF 应用程序)

问题描述

我想用 RowFilter 过滤我的 DataGrid。用户应该能够通过选择一个单元格来选择他的列。比他把一些文本放在一个文本框中,他可以过滤数据网格。我尝试了一些东西,但它们没有用。也许我可以在这里得到一些帮助 :) 我会很高兴收到每一个回复。这是我的代码和我尝试过的事情:

private void Filter_Click(object sender, RoutedEventArgs e)
        {
            DataView DV1 = DT1.DefaultView;        // DT1 is my DataTable-Object
            // DV1.RowFilter = "Column1 = '" + Filter.Text + "'";   This works fine
            DV1.RowFilter = "'" + DataGrid1.CurrentCell.Column+ "' = '" + Filtern.Text + "'"; // When i try this it doesnt work
            DataGrid1.ItemsSource = DV1;
        }

我尝试了其他一些命令:DataGrid1.CurrentCell.Column.DisplayIndex 或 DataGrid1.CurrentCell.Column.Header 或 DataGrid1.CurrentColumn 但我总是得到一个错误。指挥部给了我一个 0。也许有人有想法?

标签: c#wpfdatagridrowfilter

解决方案


尝试这个:

DataGrid1.SelectedCells[0].ColumnIndex

0 是选择的第一个元素


推荐阅读