【问题标题】:How can I filter dataGridView by checking multiple items in CheckedListBox?如何通过检查 CheckedListBox 中的多个项目来过滤 dataGridView?
【发布时间】:2016-10-05 19:19:50
【问题描述】:

我有这段代码可以使用checkedListBox 过滤我的dataGridView。每次用户选中checkedListBox中的一个框时,dataGridView都会自动更新以仅显示与选中名称相关的数据(例如,按选中名称=“John”过滤)并且效果很好。

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
                DataTableCollection tables = myDatabaseDataSet.Tables;
                DataView view = new DataView(tables[0]);
                BindingSource source = new BindingSource();
                source.DataSource = view;
                dataGridView1.DataSource = source;
                source.Filter = "myColumnName ='" + checkedListBox1.Text.Replace("'", "''") + "'";
        }

现在的问题是,我如何才能检查 checkedListBox 中的多个项目,然后通过只显示检查的名称来更新 dataGridView(例如,checkedListBox 中的检查名称是“John”和“Jane”)?

上面的代码给了我以下结果:

Code Above

我想要实现的是这个(模拟图片):

Desired outcome

感谢任何帮助。

【问题讨论】:

  • 名字不能是约翰*和简。使用或
  • 必须有一个初级排序(你不能平等地按 John 和 Jane 排序)你可以按 John 排序,然后是二级排序由 Jane;这就是你所追求的吗?
  • 我正在寻找的是过滤所有名称的datagridview,但在checkedListBox中有复选标记的名称。上面的代码已经一次带了一个名字。我一次想要多个名字。
  • @StevenByrne 是的,只要 datagridview 在同一个 datagridview 列表中同时显示两个名称。
  • 如果 kalamazoowho 的回答不能解决您的问题,请用期望的结果更新您的问题,因为我对您想要实现的目标有点困惑。是否要更改列或对数据行进行排序?如果您尝试按姓名排序,John,然后 Jane,然后 Bill,Katie 将不同于您希望 John Jane Bill 和 Katie 都显示和排除所有其他数据。

标签: c# checkedlistbox


【解决方案1】:

因此,您将拥有一个数据库,该数据库将导致某种形式的“从数据库中选择不同的名称”来填充选中的列表框。所以现在你必须添加类似的东西:

List<string> names = new List<string>();
for (int i = 0; i < checkedListBox.Items.Count; i++) 
{
    CheckState st = checkedListBox.GetItemCheckState(checkedListBox.Items.IndexOf(i));
    if(st == CheckState.Checked)
    {
        int selected = checkedListBox.SelectedIndex;
        if (selected != -1)
        {
            names.Add(checkedListBox.Items[selected].ToString());
        }
    }
}  

其结果将是checkedListBox 中已检查项目的列表。然后,您可以将其与我之前提供的代码一起使用以进行过滤。只需将硬编码的名称替换为选中的列表字符串即可。

string filterString = "";
int count = 0;
foreach (name in names)
{
    if (count != 0)
    {
        filterString += " OR Responsible = '" + name + "'";
    }
    else
    {
        filterString += "Responsible = '" + name + "'";
    }
    count += 1;
}

现在您有了一个字符串,可以用作创建 DataView 的过滤器:

DataView view = new DataView(tables[0], filterString, "Responsible Desc", DataViewRowState.CurrentRows);

这应该根据复选框状态在表格变为 DataView 时过滤表格,而不是之后。

决赛:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    DataTableCollection tables = myDatabaseDataSet.Tables;

    //
    // Place all my code here
    //

    BindingSource source = new BindingSource();
    source.DataSource = view;
    dataGridView1.DataSource = source;
}

【讨论】:

  • 这个想法是让最终用户根据需要使用checkedListBox按名称过滤表。名称列表因 Access 中的内容而异,因此我不能在列表中包含硬编码的名称。
  • 请检查我更新的问题。我需要最终用户选中相应的框,然后过滤 dataGridView。
  • 我在上面重新排序了我的答案。它应该完全符合您的要求。将它放在任何时候选中或取消选中框时都会调用的事件中。我相信这是您在其中发布代码的事件。
猜你喜欢
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多