How to Insert Object Type in ListView?

后端 未结 3 506
长发绾君心
长发绾君心 2021-02-04 11:32

I want to maintain both ID and Object Type in my ListView. I\'m trying to do this:

lstView.Items.Insert(MyObject);
// can\'t do this, because it takes only Int          


        
3条回答
  •  半阙折子戏
    2021-02-04 12:05

    Quickest way around this is to keep a list of your object on the side:

    List list = ... ; // my list
    

    Generate a dictionary from the list with a string as the ID, or you can use the index to retrieve from the original list:

    Dictionary listBinder = new Dictionary(
        list.Select(i => new KeyValuePair(i.ID, i.Name))
    );
    

    Bind or codebehind attaching the listview to the dictionary, then use the selected item to retrieve your object from your private list.

提交回复
热议问题