如何筛选的DataGridView上的多种选择依据是什么?

问题描述 投票:1回答:1

我有一个名为dgvAllBikes GridView控件。上负载形式我加载dgvAllBikes。现在我想筛选此GridView控件这是对的三个选项基础和搜索按钮dgvAllBikes。

1)CC

2)模型

3)颜色

当我在搜索按钮点击我要在这三个选项的基础过滤dgvAllBikes。

这是我的LoadGrid代码

 private DataTable LoadGrid()
    {

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("spGetAllBikes", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            dtAllBike = new DataTable();

            dtAllBike.Load(r);

        }
        return dtAllBike;
    }

我已经宣布一流水平的DataTable

string CS;
    protected DataTable dtAllBike;
    public SaleBike()
    {
        InitializeComponent();
        CS = ConfigurationManager.ConnectionStrings["BikeDB"].ConnectionString;
    }

这里是图片enter image description here

下面是btnSearch的代码。

private void btnSearch_Click(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dgvAllBikeDetails.DataSource;
        bs.Filter = dgvAllBikeDetails.Columns["CC"].HeaderText.ToString() + " LIKE '%" + tbCCSearch.Text + "%'";
        dgvAllBikeDetails.DataSource = bs;

它过滤的CC的基础上,但我不能够结合另外两个选项。您的帮助将是非常赞赏。

c# winforms gridview datagridview
1个回答
5
投票

使用ORAND运营商创建一个过滤器上的多个值的作品。

private void btnSearch_Click(object sender, EventArgs e)
{
    BindingSource bs = new BindingSource();
    bs.DataSource = dgvAllBikeDetails.DataSource;   

    string filter = "";

    // Check if text fields are not null before adding to filter. 
    if (!string.IsNullOrEmpty(tbCCSearch.Text)) 
    {
        filter += dgvAllBikeDetails.Columns["CC"].HeaderText.ToString() + " LIKE '%" + tbCCSearch.Text + "%' ";
    }
    if (!string.IsNullOrEmpty(tbModelSearch.Text)) 
    {
        if (filter.length > 0) filter += "OR ";
        filter += dgvAllBikeDetails.Columns["Model"].HeaderText.ToString() + " LIKE '%" + tbModelSearch.Text + "%' ";
    }
    if (!string.IsNullOrEmpty(tbCCSearch.Text)) 
    {
        if (filter.length > 0) filter += "OR ";
        filter += dgvAllBikeDetails.Columns["Color"].HeaderText.ToString() + " LIKE '%" + tbColorSearch.Text + "%' ";
    }

    bs.Filter = filter;
    dgvAllBikeDetails.DataSource = bs;
}
© www.soinside.com 2019 - 2024. All rights reserved.