【问题标题】:Refresh listbox after adding添加后刷新列表框
【发布时间】:2013-03-06 15:36:43
【问题描述】:

我在 C# 中有一个 listbox,并希望在我添加一个新项目后刷新它(通过新的表单对话框打开)

这是我的代码,它不起作用。

    private void showAllItems()
    {
        itemList = Db.getAllItems();
        lb_itemList.DataSource = itemList;
    }

    private void showItemPreview(object sender, EventArgs e)
    {
        string curItem = lb_itemList.SelectedItem.ToString();
        briefPreviewList = Db.getItemBriefPreview(curItem);
        string itemInfos = string.Join(",", briefPreviewList.ToArray());
        string[] infos = itemInfos.Split(',');
        l_itemDB.Text = curItem;
        l_CategoryDB.Text = infos[0];
    }

    private void b_addItem_Click(object sender, EventArgs e)
    {
        int uid = 1;
        AddItem addItemForm = new AddItem(uid);
        addItemForm.ShowDialog();
        CurrencyManager cm = (CurrencyManager)BindingContext[itemList];
        cm.Refresh();

    }

【问题讨论】:

  • my code which doesn't work 是什么意思? ListBox不更新,或者你得到一个例外还是什么?
  • @nolonar - 列表框不会更新,但也不例外。但是 cr41g14 的回答有所帮助:)

标签: c# winforms listbox


【解决方案1】:

我假设当您插入一个新项目时,它会存储到数据库中,如果是这种情况,那么您需要做的就是重置数据源:

private void b_addItem_Click(object sender, EventArgs e)
{
    int uid = 1;
    AddItem addItemForm = new AddItem(uid);
    addItemForm.ShowDialog();
    addItemForm.Dispose();

    this.showAllItems();
 }

【讨论】:

  • 啊,dispose函数帮了我很多,非常感谢:)
猜你喜欢
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
相关资源
最近更新 更多