Link values ​​from list array to list

Can any authority give a short example for binding a value from a list array to a list in C # .net

+3
source share
1 answer

It depends on how your list array is.

Start with a simple example:

List<string> listToBind = new List<string> { "AA", "BB", "CC" };
this.listBox1.DataSource = listToBind;

Here we have a list of rows that will be displayed as elements in the list.

alt text

Otherwise, if your list items are more complex (e.g. custom classes), you can do it like this:

Having, for example MyClass, defined as follows:

public class MyClass
{
    public int Id { get; set; }
    public string Text { get; set; }
    public MyClass(int id, string text)
    {
        this.Id = id;
        this.Text = text;
    }
}

here is the required part:

List<MyClass> listToBind = new List<MyClass> { new MyClass(1, "One"), new MyClass(2, "Two") };
this.listBox1.DisplayMember = "Text";
this.listBox1.ValueMember = "Id"; // optional depending on your needs
this.listBox1.DataSource = listToBind;

, . ValueMember listBox1.SelectedValue, Id, .

N.B.
DisplayMember unset, ToString() ListBox.

alt text

+12

Source: https://habr.com/ru/post/1776905/


All Articles