c# add object to listbox and show string of object in it

后端 未结 4 1842
情歌与酒
情歌与酒 2020-12-09 08:51

I\'m using a ListBox and adding objects to it.

The object contains 2 variables, let\'s say username and userid.

How can I add the objects in the list (like l

4条回答
  •  没有蜡笔的小新
    2020-12-09 09:38

    Set the DataTextField and DataValueField

    ListBox a = new ListBox();
    a.DataTextField = "username";
    a.DataValueField = "userid";
    

    To add using .Add method you can use:

    a.Items.Add(new ListItem() { Value = User.ID, Text = User.Name });
    

提交回复
热议问题