使用TextBox动态过滤DataGrid的名称
首先是我的问题:我喜欢动态地使用TextBox
过滤我的DataGrid
以获取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); }
它看起来像这样:
将您的代码更改为:
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; }