Hello I am trying to iterate through the selected items of a listbox in winforms like this:
foreach (ListBox item in sknBox.SelectedItems)
{
Console.WriteLine(item.ValueMember);
Console.WriteLine(item.DisplayMember);
}
But I get the following error:
Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Forms.ListBox'.
Is there a way to fix this? or another straight forward solution?
From the error message it is clear that the
.SelectedItemsis a collection ofSystem.Data.DataRowView, so you can access the required values throughRowwith indexers as like the following: