Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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. Basically i add rows when i run my app otherwise there is nothing in it.

Onclick Add Method for Listview:

I create a class name AddList and its default constructor gets 4 argument. Like
AddList ad = new AddList("1","3","2","5");
So when i click on add button:

void private addBtn_Onclick(default arguments..........){
listview.Items.Add(ad);
}

Now i want to save all added items in listview in DB. I am using SQL Server. That is why i need to loop each item in listview to insert each item in DB one by one.
Posted
Updated 25-Nov-14 18:28pm
v2
Comments
DamithSL 25-Nov-14 22:21pm    
what is the error you get?
and also include the code how you add items to your listview
Burhan47 25-Nov-14 22:39pm    
I add item with a button with onclick event. listview.items.Add("Add") etc. And items successfully add and i can see items added in Listview. Only issue is that how i can each column and rows value with loop. I found many sites which uses ListviewItem object to loop listview items and subitem and some uses for loop. I tried both foreach and for loop but error is coming. Basically i want to save item value in string so i use String abc = listview.items[i].Text; inside a loop. Error is coming on .Text and on type of variable. I am using WPF C#
DamithSL 25-Nov-14 22:44pm    
please update the question with your code how you add items with sub items
Burhan47 26-Nov-14 1:18am    
if i use something like this inside loop:
listView1.Items[i].SubItems[2].Text
It gives error and .Text and SubItems underlined with red color

try like below
C#
for (int i = 0; i < listview.Items.Count; i++)
{
    AddList item  = listview.Items[i] as AddList;
    // then you can access properties of item.PropertyName
}
 
Share this answer
 
v2
Comments
Burhan47 25-Nov-14 22:40pm    
.Text is underlined with red color and also gives error on string type and say it doesnt support string bcz its a object
Burhan47 25-Nov-14 23:46pm    
VS2013 not showing Text property of listview instead it is showing TextInput, that is why it is giving error
Burhan47 26-Nov-14 0:38am    
i think this method is not compatible with WPF
DamithSL 26-Nov-14 1:26am    
check my updated answer
Burhan47 26-Nov-14 2:19am    
i dont want to add them. They already added in listview. I want to retrieve each item one by one. And by the way listview.Items[i] and listview.Items[i].SubItem[0] is not working
C#
foreach (var item in listView.Items)
{
   //Do stuff
}
 
Share this answer
 
Comments
Burhan47 25-Nov-14 22:44pm    
error and warning comes when i add something inside loop. I add inside loop
string subitem1 = listview.Items[i].SubItems[0].Text; but it start showing warning

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900