如何在我的 DataGridC# WPF 应用程序中获取所选单元格的当前列

问题描述

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

M2 = aliased(Message)
q = session.query(Message.form_id.distinct()).filter(
    ~session.query(M2)
    .filter(Message.form_id == M2.form_id)
    .filter(M2.active == False)
    .exists()
)

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

解决方法

从 DataGridView 获取当前列名:

int columnIndex = DataGrid1.CurrentCell.ColumnIndex;
string columnName = DataGrid1.Columns[columnIndex].Name;

接下来,您的 RowFilter 不正确,列名不应用引号 ' 括起来。

DV1.RowFilter = <<ColumnName>> + " = '" + Filtern.Text + "'";

最后,您的代码应如下所示:

private void Filter_Click(object sender,RoutedEventArgs e)
{
    DataView DV1 = DT1.DefaultView;
    
    // Get column name
    int columnIndex = DataGrid1.CurrentCell.ColumnIndex;
    string columnName = DataGrid1.Columns[columnIndex].Name;

    DV1.RowFilter = String.Format("{0} = '{1}'",columnName,Filtern.Text); 
    DataGrid1.ItemsSource = DV1;
}

注意:这种过滤方式仅适用于具有 string 值的列。

,

试试这个:

DataGrid1.SelectedCells[0].ColumnIndex

0 是第一个被选中的元素

,

谢谢你们!我明白了:)

这是我的代码,希望能帮到其他人:

int columnIndex = DataGrid1.SelectedCells[0].Column.DisplayIndex; 
string columnname = Convert.ToString(DataGrid1.Columns[columnIndex].Header);
string fullcommand = string.Format("{0} = '{1}'",columnname,Filtern.Text);
DataView DV1 = DT1.DefaultView; 
DV1.RowFilter = fullcommand;
DataGrid1.ItemsSource = DV1;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...