在不重新加载数据的情况下过滤DataGridView的内容

时间:2015-09-17 20:39:50

标签: c# datagridview

我有一个MysqlTable适配器,并且像这样使用数据集:

 string sql = "SELECT firstName,lastName,phoneNumber FROM customers";
            daCustomers = new MySqlDataAdapter(sql, conn);
            MySqlCommandBuilder cb = new MySqlCommandBuilder(daCustomers);

            dsCustomers = new DataSet();
            daCustomers.Fill(dsCustomers, "customers");
            dataGridView1.DataSource = dsCustomers;
            dataGridView1.DataMember = "customers";

有没有办法在不重新加载数据的情况下过滤dataGridView中显示的内容?

2 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点..例如创建一个返回DataSet的方法

  public DataSet GetCustomers()
  {
    SqlDataAdapter adapter = new SqlDataAdapter(
      "SELECT firstName,lastName,phoneNumber FROM customers", conn);

    DataSet custDS = new DataSet();
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    adapter.Fill(custDS, "Customers");

    return custDS;
  }

如果您返回DataTable

public DataTable GetCustomers()
{
    SqlDataAdapter adapter = new SqlDataAdapter(
      "SELECT firstName,lastName,phoneNumber FROM customers", conn);

    DataSet custDS = new DataSet();
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    adapter.Fill(custDS, "Customers");

    return custDS.Tables[0];
}

答案 1 :(得分:0)

您可以绑定DataView而不是DataTable,并在那里设置过滤字符串(属性.RowFilter)。