在WPF C#中迭代listview项目 [英] Iterate listview items in WPF C#

查看:46
本文介绍了在WPF C#中迭代listview项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在数据库中的listview控件中保存行,但我无法遍历每一行。我的listview是多列,它有4列。我试过循环:



for(int i = 0; i< listview.Items.Count; i ++)

{

字符串abc = listview.Items [i] .Subitem [i] .Text;

//其他可变资料

}



但它不起作用并且给出错误。那么在WPF中迭代Listview项的最佳方法是什么。基本上我在运行我的应用程序时添加行,否则它没有任何内容。



Onclick为Listview添加方法:



我创建一个类名AddList,它的默认构造函数得到4个参数。喜欢

AddList ad = new AddList(1,3,2,5);

所以当我点击添加按钮时:



void private addBtn_Onclick(默认参数..........){

listview.Items.Add(ad);

}



现在我想在DB中保存listview中的所有添加项目。我正在使用SQL Server。这就是为什么我需要循环列表视图中的每个项目以逐个插入数据库中的每个项目。

解决方案

尝试如下

< pre lang =cs> for int i = 0 ; i < listview.Items.Count; i ++)
{
AddList item = listview.Items [i] < span class =code-keyword> as AddList;
// 然后你可以访问item.PropertyName的属性
}


  foreach  var  item  in  listView.Items)
{
// 做东西
}


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.

解决方案

try like below

for (int i = 0; i < listview.Items.Count; i++)
{
    AddList item  = listview.Items[i] as AddList;
    // then you can access properties of item.PropertyName
}


foreach (var item in listView.Items)
{
   //Do stuff
}


这篇关于在WPF C#中迭代listview项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆