添加后刷新列表框

时间:2013-03-06 15:36:44

标签: c# winforms listbox

我在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();

    }

1 个答案:

答案 0 :(得分:1)

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

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

    this.showAllItems();
 }