过滤DataGridView Winforms

时间:2015-11-03 09:04:22

标签: c# winforms datagridview

我正在尝试创建一个按钮,它接受2个用户输入,然后根据两个输入过滤datagridview。我正在使用winforms和Sql。以下是我发现并尝试实现的一些代码,但它没有使用过滤后的数据填充datagridview。

        private void button3_Click(object sender, EventArgs e) {
        dataSet31.Personal_Details.Clear();
        using (SqlDataAdapter sqlDataAdapter =
new SqlDataAdapter(sqlCommand2.CommandText = "select * from Personal_Details WHERE '" + comboBox2 + "' LIKE '" + textBox1 + "'",
    "Data Source=Z46308;Initial Catalog=VSTest;Integrated Security=True"))
        {
            using (DataTable dataTable = new DataTable())
            {
                sqlDataAdapter.Fill(dataTable);
                this.DataGridView01.DataSource = dataTable;
            }
        }
    }

4 个答案:

答案 0 :(得分:2)

如果您在表单加载中加载数据,那么您不需要运行查询来过滤网格,您只需使用DataView来过滤数据表。

在下面的示例中,我假设您使用comboBox2来选择要根据其过滤的列名。

//Create a data table to store data when you load them in form load
private DataTable dataTable;
private void Form1_Load(object sender, EventArgs e)
{
    dataTable = new DataTable();
    string constring = @"Data Source=Z46308;Initial Catalog=VSTest;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("select * from Personal_Details", con))
        {
            //Fill the data table here
            sda.Fill(dataTable);
        }
    }
    //Set the data source of grid
    this.DataGridView01.DataSource = new DataView(dataTable);
}
private void button3_Click(object sender, EventArgs e)
{
    //Get the datasource from grid
    var dv = (DataView)this.DataGridView01.DataSource;

    //comboBox2.SelectedItem or comboBox2.SelectedValue based on your settings
    //Apply filter to data source
    dv.RowFilter = string.Format("{0} Like '%{1}%'",comboBox2.SelectedItem,   textBox1.Text);
}

修改

您还可以使用this.DataGridView01.DataSource = dataTable;设置数据源,然后在过滤时只使用dataTable.DefaultView.RowFilter = ....

答案 1 :(得分:1)

您无需一直在数据库上运行查询。这是另一种方法。

您可以使用DataView.RowFilter属性来过滤内存中的数据。完成此操作后,您可以将数据重新绑定到网格中。假设数据表对象名称为table,则可以在按钮单击中执行此操作。

        table.DefaultView.RowFilter = combobox1.SelectedText + " LIKE '%" + textbox1.Text + "%'";
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = table.DefaultView;

答案 2 :(得分:0)

尝试将comboBox2更改为comboBox2.SelectedValue,将textBox1更改为textBox1.Text。 我不认为查询会以你现在的方式返回任何结果。

我在我的应用程序中使用类似的东西,查看我的代码,看看你是否可以调整它以满足你的需求:

SqlCommand cmd = new SqlCommand("SELECT o.OrderID AS ID,o.ProductID AS ProductID, p.ProductName AS 'ProductName', s.CompanyName AS Supplier, c.CategoryName AS Category, o.UnitPrice AS 'Unit Price', o.Quantity AS Quantity, (o.UnitPrice * o.Quantity) AS 'Sub-Total'" +
          "FROM [Order Details] o INNER JOIN Products p ON o.ProductID = p.ProductID " +
          "INNER JOIN Suppliers s ON p.SupplierID = s.SupplierID INNER JOIN Categories c ON p.CategoryID = c.CategoryID WHERE OrderID = '" + DropDownList1.SelectedValue + "'", new SqlConnection(ConfigurationManager.ConnectionStrings["databaseConnection1"].ConnectionString));
    cmd.Connection.Open();
    SqlDataReader sdrEmp = cmd.ExecuteReader();
    try
    {
        if (sdrEmp.HasRows)
        {
            DataTable dt = new DataTable();
            dt.Load(sdrEmp);
            //Response.Write(dt);
            ViewState["CurrentTable"] = dt;
            dataGridView1.DataSource = dt;
            dataGridView1.DataBind();        // BIND DATABASE TABLE WITH THE GRIDVIEW.
        }
    }

答案 3 :(得分:0)

 private void button2_Click(object sender, EventArgs e)
    {
        //dataSet31.Personal_Details.Clear();
        SqlCommand cmd = new SqlCommand();
        using (SqlDataAdapter sqlDataAdapter =
new SqlDataAdapter(cmd.CommandText = "select * from vtypes WHERE VoucherType ='" + comboBox1.Text + "' AND VouOrder = '" + textBox1.Text + "'",
    "Data Source=SQLEXPRESS;Initial Catalog=DB50A0;Integrated Security=True"))
        {
            using (DataTable dataTable = new DataTable())
            {
                sqlDataAdapter.Fill(dataTable);
                this.dataGridView1.DataSource = dataTable;
            }
        }
    }