Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c# - Filter DataGrid for name with TextBox dynamically

First to my problem: Id like to filter my DataGrid with a TextBox dynamically for the Name.

My current code:

how the DataGrid is filled:

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;

The Code for the 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);
}

It looks like this:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your code to this:

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...