[英]C# Last Item of the checkedListBox is not showing up
此代碼預過濾dataGridView,因此僅顯示列表中的已檢查項。
我面臨的問題是代碼每次都會忽略最后一項,除非我取消選中,再次檢查並按“Go”。
這是我的代碼:
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;
}
我知道解決方案可能很愚蠢,但我無法弄清楚。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.