使用TextBox动态过滤DataGrid的名称

时间:2015-12-11 10:46:18

标签: c# mysql wpf filter datagrid

首先是我的问题:我想为DataGrid动态过滤TextBox动态Name

我目前的代码:

如何填充DataGrid

cnb1 = new MySqlConnectionStringBuilder();
cnb1.Server = "xxxxxxxxxxxxxxxxxxx";
cnb1.Database = "xxxxxxxxxxxxxxx";
cnb1.UserID = "xxxxxxxxxxxxxxxxx";
cnb1.Password = "xxxxxxxxxxxxxxxxxx";
cn1 = new MySqlConnection(cnb1.ConnectionString);
cn1.Open();
da = new MySqlDataAdapter("SELECT * FROM AktuelleDaten", cn1);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.SelectCommand.ExecuteNonQuery();
ds = new DataSet();
da.Fill(ds);
hauptübersichtgrid.ItemsSource = ds.Tables[0].DefaultView;

TextBox的代码:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    DataSet DV = ds;
    hauptübersichtgrid.ItemsSource = DV.Tables[0].DefaultView.RowFilter = string.Format("Name like '%{0}%'", Filter.Text);
}

看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

private void TextBox_TextChanged(object sender, EventArgs e)
{
    DataView dv = ds.Tables[0].DefaultView;
    dv.RowFilter = string.Format("Name like '%{0}%'", Filter.Text);
    hauptübersichtgrid.ItemsSource = dv;
}