如何分组和rowFilter一列

时间:2015-04-02 13:36:12

标签: c# winforms datagridview

我的datagridview从数据库中获取记录,如下所示:

[Country]     
  Gh
  Ng
  Sa
  Gm
  Sw
  Ru

我想现在使用组合框过滤此列。我的组合框中有两个值 非洲和欧洲。我的期望是得到这样的东西:

//If i select Africa in the combobox
  Gh
  Ng
  Sa
// And if i select Europe
  Gm
  Sw
  Ru

到目前为止,这是我的代码:

private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
    {

            try
            {
                //Check an see what's in the dgv
          DataView dv = new DataView(dt);
          dv.RowFilter = "[Country]  LIKE '%" + combobox1.Text.Trim() + "%'";
          datagridview1.DataSource = dv;
            }
            catch (Exception)
            {
    MessageBox.Show("Column not found", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        }

我如何对它们进行分组并实施呢?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
  //search for identical value
  if (row.Cells[0].Value.ToString().Equals(searchValue))//searchvalue would be set using ComboBox.Text
  {
    dataGridView1.Rows[0].Visible = true;
  }
  //search for first art that contains search value
  else
  {
    dataGridView1.Rows[0].Visible = false;
  }
}

它可能不完全正确,因为我必须在没有编译器的情况下快速完成此操作