简体   繁体   中英

Searching for name in dataGridView through more then one table in C#

I have a browse button witch browses 5 different tables into DataGridView . I have a scrollBar to go through them. Now I need to make a search TextBox and if I write in it something like "Milk" it automatically finds all tables witch contain that name/word ex there were 5 tables to go through but now only 3, because only 3 contain word milk. Here is my code witch searches only in 1 table (I only got that far). Thank you!

private void textBox5_TextChanged(object sender, EventArgs e)
{
    DataView dv = new DataView(dt);
    dv.RowFilter = string.Format("Prece LIKE '%{0}%'", textBox5.Text);
    dataGridView1.DataSource = dv;
}

What about something like this:

private void textBox5_TextChanged(object sender, EventArgs e)
{
     // Your gridViews here
     var gridList = new List<DataGridView>() { yourGrid1, yourGrid2};

     foreach(DataGridView dv in gridList)
     {
         dv.RowFilter = string.Format("Prece LIKE '%{0}%'", textBox5.Text);
         dataGridView1.DataSource = dv;
     }
}

Add your DataGridViews to the List and let it iterate through them. You just have to handle how to output it then.

Note that I havn't edited your searching-method in here. I've just added the loop.

If you have one DataGridView and all DataTables are same

private void textBox5_TextChanged(object sender, EventArgs e)
        {           
            DataView dv = new DataView(dt);
            dv.RowFilter = string.Format("Prece LIKE '%{0}%'", textBox5.Text);

            DataTable dtMain=dv.ToTable().Copy();

            dv = new DataView(dt2);
            dv.RowFilter = string.Format("Prece LIKE '%{0}%'", textBox5.Text);
            dtMain.Merge(dv.ToTable());
            dv = new DataView(dt3);
            dv.RowFilter = string.Format("Prece LIKE '%{0}%'", textBox5.Text);
            dtMain.Merge(dv.ToTable());

            dataGridView1.DataSource = dtMain;
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM