Search code examples
c#arraysloopslistviewdatarowview

How do I send SelectedItems to a DataRowView array?


I'm creating a medical database application as the final project for my course, and I need to get each selected item in a listview checkbox thingo to a DataRowView array so I can insert each one into a database. I'm trying to do this via a loop but when it displays the textbox the actual day isn't changing and it's just displaying the day name of the first day I selected

DataRowView[] availDays = new DataRowView[lvDocAvail.Items.Count];
int i = 0;

foreach (DataRowView drv in lvDocAvail.SelectedItems)
{
    availDays[i] = (DataRowView)lvDocAvail.SelectedItem;
    MessageBox.Show(availDays[i].Row["nameofDay"].ToString());
    i++;
}

Solution

  • Just replace this line

    availDays[i] = (DataRowView)lvDocAvail.SelectedItem;
    

    With

    availDays[i] = (DataRowView)drv;
    

    The problem is that you're looping over selected items but each time you ignore your loop variable and use the first selected item.