此代码预过滤dataGridView,因此仅显示列表中的已检查项目。
我面临的问题是代码每次都会忽略最后一项,除非我取消选中,再次检查并按“开始”。
这是我的代码:
public partial class Notifications : Form
{
string filterstring = "";
int count = 0;
private void checkedListBox_ItemCheck(object sender, System.EventArgs e)
{
// Loop through all items in the checkedBoxes.
foreach (object itemChecked in checkedListBox.CheckedItems)
{
if (count != 0)
{
filterstring += "OR Responsible = '" + itemChecked.ToString() + "'";
}
else
filterstring += "Responsible = '" + itemChecked.ToString() + "'";
count += 1;
}
}
private void button1_Click(object sender, EventArgs e)
{
DataTableCollection tables = myDatabaseDataSet.Tables;
DataView view = new DataView(tables[0]);
BindingSource source = new BindingSource();
source.DataSource = view;
dataGridView1.DataSource = source;
source.Filter = filterstring;
}
我知道解决方案可能很愚蠢,但我无法理解。
答案 0 :(得分:0)
private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
filterstring = "Responsible = '" + checkedListBox.Items[e.Index].ToString() + "' OR";
// Loop through all items in the checkedBoxes.
foreach (object itemChecked in checkedListBox.CheckedItems)
{
filterstring += " Responsible = '" + itemChecked.ToString() + "' OR";
}
filterstring = filterstring.Substring(0, filterstring.LastIndexOf("OR"));
}
您必须检查项目的新值是否被点击。如果选中它,您可以将其包含在过滤字符串中。删除最后一个OR比在每个迭代器中更好。 编辑:你的事件是EventArgs Bu应该是ItemCheckEventArgs