• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

adding a value and text to a listbox

Hi,

I normally work with asp.net but am having to work with a winform.

I need a control that is like the listbox but where I can set a different Id to the text display, like a dropdown for instance.

Is this possible?
0
scm0sml
Asked:
scm0sml
  • 2
  • 2
1 Solution
 
code_meCommented:
can you please
explain your
requirement
more clearly
0
 
scm0smlAuthor Commented:
I have this code:
 Private Sub PopulateDepartments()
        Dim dr As DataRow = Nothing
        Dim li As ListViewItem = Nothing

        For Each dr In ReportHelper.GetDepartments.Rows

            'lstDept.Items.Add()
        Next
    End Sub

Within there I would like to add items to my listbox, displaying the department but when the user clicks on it I want to be able to access the dept id.

In the same way as if I used a dropdownlist but I want to be able to display the departments in a listbox view.
0
 
Pratima PharandeCommented:
List<Person> people = new List<Person>(); 
           people.Add(new Person { Age = 25, FirstName = "Alex", LastName = "Johnson" });                   people.Add(new Person { Age = 23, FirstName = "Jack", LastName = "Jones" });                     people.Add(new Person { Age = 35, FirstName = "Mike", LastName = "Williams" });                  people.Add(new Person { Age = 25, FirstName = "Gill", LastName = "JAckson" });                   this.listBox1.DataSource = people;              
           this.listBox1.DisplayMember = "FirstName";  
            this.listBox1.ValueMember = "Age";

Open in new window

0
 
scm0smlAuthor Commented:
So I could do this then:
Dim dr As DataRow = Nothing
        Dim li As ListViewItem = Nothing

        lstDept.DataSource = ReportHelper.GetDepartments
        lstDept.DisplayMember = "Department"
        lstDept.ValueMember = "DepartmentID"
0
 
Pratima PharandeCommented:
Change text and value with row values as you want

 Private Sub PopulateDepartments()
        Dim dr As DataRow = Nothing
        Dim li As ListViewItem = Nothing

        For Each dr In ReportHelper.GetDepartments.Rows
lstDept.Items.Add(New ListItem("Text", "value")),

        Next
    End Sub
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now