C# removing items from listbox

后端 未结 16 2296
礼貌的吻别
礼貌的吻别 2020-12-02 00:12

I have a listbox being populated from a SQLDATA pull, and it pulls down some columns that i dont want like OBJECT_dfj, OBJECT_daskd. The key is all of these being with OBJE

16条回答
  •  时光说笑
    2020-12-02 00:42

    I found out the hard way that if your listbox items are assigned via a data source

    List workTables = hhsdbutils.GetWorkTableNames();
    listBoxWork.DataSource = workTables;
    

    ...you have to unbind that before doing the removal:

    listBoxWork.DataSource = null;
    for (int i = listBoxWork.Items.Count - 1; i >= 0; --i)
    {
        if (listBoxWork.Items[i].ToString().Contains(listboxVal))
        {
            listBoxWork.Items.RemoveAt(i);
        }
    }
    

    Without the "listBoxWork.DataSource = null;" line, I was getting, "Value does not fall within the expected range"

提交回复
热议问题