How to iterate (list) all selected items in a Databound ListBox in .NET 4.0 Winforms


A seemingly easy thing to do that I could not find documentation for!

string astr;
foreach (DataRowView aRow in listBox1.SelectedItems )
{
    astr = aRow[“TopicId”].ToString();
    astr = aRow[“TopicText”].ToString();
}

TopicId and TopicText are the name of the columns in the table of the Datasource that has been bound to the listbox control.

Drop me a note if you found this useful!


Comments (3)

  1. mitch says:

    Thanks! Just wasted an hour trying to figure this out. Your solution worked perfectly. Thanks!

  2. James says:

    Thank Darwin for this post. I spent AGES looking for this functionality! THANK YOU!

  3. F. Gabriel says:

    Awesome, simple and effective.

    Thanks!