【问题标题】:How to filter datagridview using multiple checkboxes如何使用多个复选框过滤 datagridview
【发布时间】:2015-04-14 13:16:54
【问题描述】:

我有一个 datagridview 从数据库获取数据,还有三个 checkBoxes 应该用作过滤器。

我想通过检查checkedboxes 中的一项或多项来过滤datagridview,并应在datagridview 中向我显示所选项目及其相关金额:

我的代码:

           if (cb11.Checked == true)
        {
            try
            {
                //Check an see what's in the dgv
                DataView dv = new DataView(dt);
                dv.RowFilter = " [AreaCode] = " + cb11.Text.Trim();
                datagridview1.DataSource = dv;
            }
            catch (Exception)
            {
                MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

这只会给我 11 的值 我怎样才能加入另一个,以便我可以选择多个?

【问题讨论】:

  • 听起来不错。你还在等什么?
  • 请告诉我们你尝试了什么?以及您面临的问题
  • 嗨 zohar,我期待 excel 过滤(动态)之类的东西:如果我检查 16 和 31,我希望 datagridview 中的结果是:11 5.660.596 5.849.473. -188.877.12531 88.326.252 88.702.273 -376.021.097
  • 好的。这就是我到目前为止所拥有的,但它不起作用:if (cb11.Checked == true) { try { //Check an see what's in the dgv DataView dv = new DataView(dt); dv.RowFilter = " [AreaCode] = " + cb11.Text.Trim(); datagridview1.DataSource = dv; } catch (Exception) { MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } }

标签: c# winforms datagridview


【解决方案1】:

没有看到你尝试过的代码,我只是解释一下你可以尝试什么。

假设您只从数据库中获取数据一次:

  1. 从数据库中获取数据并将其存储在列表中
  2. 每次选中/取消选中复选框

    2.1。清除您的数据网格视图

    2.2。遍历您的列表并根据选中的复选框填充您的 datagridview。

如果您要多次获取数据:

  1. 每次选中/取消选中复选框

    1.1。清除您的数据网格视图

    1.2。根据选中的复选框构造查询

    1.3。查询您的数据,并填充您的 datagridview

编辑:

因此,使用您提供的代码,尝试创建一个字符串,在评估所有复选框后将其设置为 RowFilter。

        string rowFilter = string.Empty;
        if (cb11.Checked)
        {
            rowFilter += " [AreaCode] = " + cb11.Text.Trim();
        }
        if (cb16.Checked)
        {
            if (rowFilter.Length > 0)
                rowFilter += " OR";
            rowFilter += " [AreaCode] = " + cb16.Text.Trim();
        }
        if (cb31.Checked)
        {
            if (rowFilter.Length > 0)
                rowFilter += " OR";
            rowFilter += " [AreaCode] = " + cb31.Text.Trim();
        }

        try
        {
            //Check an see what's in the dgv
            DataView dv = new DataView(dt);
            dv.RowFilter = rowFilter;
            datagridview1.DataSource = dv;
        }
        catch (Exception)
        {
            MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

【讨论】:

  • 嗨 Shar1,过滤仅适用于 cb11,如果我检查了其他过滤器,它们不会过滤。如果我选择两个或更多,它只会过滤 cb11
  • 您能否编辑您的代码帖子,以便我可以看到您在代码示例中所做的更改?
  • 谢谢 Shar1 我现在明白了。我推理有误:)
  • 嗨 Shar1,我正在尝试将您帮助我的代码从 checkedBoxes 转换为 Treeview checkboxes,但它不起作用。你能帮帮我吗?
【解决方案2】:

试试这个:

string RowFilter = string.Empty;
CreateOrAppendToFilter(cb11, ref RowFilter);
CreateOrAppendToFilter(cb16, ref RowFilter);
CreateOrAppendToFilter(cb31, ref RowFilter);
  if(RowFilter.Length > 0) 
  {
     try
        {
            //Check an see what's in the dgv
            DataView dv = new DataView(dt);
            dv.RowFilter = RowFilter;
            datagridview1.DataSource = dv;
        }
        catch (Exception)
        {
            MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
  }

    private void CreateOrAppendToFilter(CheckBox cb, ref string RowFilter) 
    { 
        if(RowFilter.Length>0) 
        {
            RowFilter += " OR ";
        }
        RowFilter += (cb.Checked) ? string.Format("[AreaCode] = {0}", cb.Text.Trim()) : string.Empty ;
    }

【讨论】:

    猜你喜欢
    • 2015-06-21
    • 2018-06-20
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多