【问题标题】:Searching for name in dataGridView through more then one table in C#通过 C# 中的多个表在 dataGridView 中搜索名称
【发布时间】:2015-08-19 09:15:50
【问题描述】:

我有一个浏览按钮,可以将 5 个不同的表格浏览到 DataGridView。我有一个scrollBar 来查看它们。现在我需要搜索TextBox,如果我在其中写下“Milk”之类的内容,它会自动找到所有包含该名称/单词的表格,例如有 5 个表要经过,但现在只有 3 个,因为只有 3 个包含单词 milk。这是我的代码女巫只在 1 个表中搜索(我只到了那么远)。谢谢!

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

【问题讨论】:

  • 表格之间的 gridView 中是否设置了任何数据关系?您如何存储该数据源?为什么要迭代gridview而不是数据库?

标签: c# search datagridview


【解决方案1】:

这样的事情怎么样:

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

将您的DataGridViews 添加到列表中并让它遍历它们。你只需要处理如何输出它。

请注意,我没有在此处编辑您的搜索方法。我刚刚添加了循环。

【讨论】:

    【解决方案2】:

    如果你有一个 DataGridView 并且所有的 DataTables 都是一样的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多