Freigeben über


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

  • Anonymous
    August 16, 2012
    Thanks! Just wasted an hour trying to figure this out. Your solution worked perfectly. Thanks!

  • Anonymous
    August 16, 2012
    Thank Darwin for this post. I spent AGES looking for this functionality! THANK YOU!

  • Anonymous
    August 21, 2015
    Awesome, simple and effective. Thanks!