2

我在 Google 中进行了一些搜索,但找不到有用的方法...请帮助...所以我有一个列表框来显示数据库中的数据,我的功能允许用户一次删除一个/多个数据,当然我想相应地刷新我的列表框。例如,我尝试 checkListBox1.Refresh(); 但没有用。请帮我。

  private void button1_Click(object sender, EventArgs e)
        {

            string item_name = "";
            foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                item_name = itemChecked.ToString();
                removefromdatabse2(item_name); // a function that update the database
                MessageBox.Show("successfully deleted");
               // checkedListBox1.Refresh();  , which didn't work

            }
        }
4

3 回答 3

2

您可以清除其项目的整个列表框并从数据库中重新填充它,或者找到要删除的项目并手动将其删除。或者,使用 ListBox 控件上的“DataSource”属性并在运行查询时更新源。

于 2012-05-11T02:41:04.473 回答
0

你有两个选择:

  • 从数据库中删除选中的ListBoxItem实例ListBox(使用ListBox.SelectedItems属性获取选中的项目)和对应的项目存储在数据库中。每个ListBoxItem实例都应该有一个唯一的标识符,该标识符与存储项目的数据库表中使用的标识符相匹配。
  • 删除数据库中存储的与 中所选项目相对应的项目ListBox,清除ListBox并重新加载数据库中存储的项目。
于 2012-05-11T02:40:31.070 回答
0

清除它,然后将列表框重新绑定到数据源

于 2012-05-11T05:17:39.307 回答