What is the proper way to load up a ListBox?

后端 未结 6 841
我寻月下人不归
我寻月下人不归 2020-12-05 05:00

What is the proper way to load a ListBox in C# .NET 2.0 Winforms?

I thought I could just bind it to a DataTable. No such luck.
I though

6条回答
  •  一个人的身影
    2020-12-05 05:18

    Lets assume your data type is called MyDataType. Implement ToString() on that datatype to determine the display text. e.g.:

    class MyDataType
    {
      public string ToString()
      {
        //return the text you want to display
      }
    }
    

    Then you can take a list consisting of your datatype and cram it into the ListBox via AddRange() as follows:

    ListBox l;
    List myItems = new List(); // populate this however you like
    l.AddRange(myItems.ToArray());
    

    Let me know if you need more help - it would help to know what datatype you are trying to display in the listbox.

提交回复
热议问题