如何通过检查 CheckedListBox 中的多个项目来过滤 dataGridView?
How can I filter dataGridView by checking multiple items in CheckedListBox?
我有这段代码可以使用 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")?
以上代码给出了以下结果:
我要实现的是这个(模拟图):
感谢任何帮助。
因此您将拥有一个数据库,该数据库将导致某种形式的 "Select distinct name from database" 来填充 checkedListBox。所以现在你必须添加如下内容:
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);
这应该根据复选框状态过滤 table 当它变成 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 过滤我的 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")?
以上代码给出了以下结果:
我要实现的是这个(模拟图):
感谢任何帮助。
因此您将拥有一个数据库,该数据库将导致某种形式的 "Select distinct name from database" 来填充 checkedListBox。所以现在你必须添加如下内容:
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);
这应该根据复选框状态过滤 table 当它变成 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;
}