I tried to save rows in listview control in database but i am not able to loop through each row. My listview is multicolumn, it have 4 column. I tried for loop:

     for (int i = 0; i < listview.Items.Count; i++)
        {
            String abc = listview.Items[i].Subitem[i].Text;
            //other varables
        }

But it is not working and giving error. So what is the best way to iterate Listview items in WPF.

Recommended Answers

All 2 Replies

well what I would do is this because there are so few columns

string abs=listview.Items[i].Subitem[0].ToString();
abs+=listview.Items[i].Subitem[1].ToString();
abs+=listview.Items[i].Subitem[2].ToString();
abs+=listview.Items[i].Subitem[3].ToString();

OR you can put in another for loop if you have more columns

for (int i = 0; i < listview.Items.Count; ++i)
    string abs="";
    for(int j=0;j<=3;++j){        {
        abs+=listview.Items[i].Subitem[j].ToString();
    }
    do something with abs
}

That will help you understand the row,column(i,j) iterations of a matrix...

What you do with the data is something else entirely, so string abs is just one big string of all the 4 columns together per row

Congratulations on using WPF.
As a WPF developer one does NOT iterate over the items in ANY control.
What you should be doing is iterating over the collection that constitutes the Items control's DataSource.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.