C#_ListView控件常用操作

将数据填充至ListView

ListViewItem item = new ListViewItem();
item.Text = "1";
item.SubItems.Add("2");
item.SubItems.Add("3");
listView1.Items.Add(item);

 

将数据填充至ListView时判断信息是否重复

foreach(ListViewItem item in this.listView1.Items)
{
   if (item.SubItems[0].Text =="1" && item.SubItems[1].Text == "2")
   {
       //信息重复...
   }
}

 

获取ListView中指定n个列的记录,存在二维数组中

//从第1列开始获取2列记录
int n=0;
int k=2;
string[,] msg =null;
if (listView1.Items.Count != 0)
{
   msg= new string[listView1.Items.Count,k];
   for(int i=0;i<listView1.Items.Count;i++)
   {
      for (int j=n;j<k;j++)
      {
         msg[i, j] = listView1.Items[i].SubItems[j].Text;
      }
   }
}

 

修改ListView中选中行的信息(FullRowSelect=True;MultiSelect=False,即整行单选模式)

if (listView1.SelectedItems.Count != 0)
{
   listView1.SelectedItems[0].SubItems[2].Text = "2";
   listView1.SelectedItems[0].SubItems[4].Text = "3";
}

 

获取ListView中选中行记录,存在ArrayList中 (整行单选模式)

System.Collections.ArrayList array = new System.Collections.ArrayList();
for (int i=0;i<listView1.Columns.Count;i++)
{
   array.Add(listView1.SelectedItems[0].SubItems[i].Text);
}

 

删除ListView中选中行记录(整行单选模式)

if (listView1.SelectedItems.Count != 0)
{
   listView1.SelectedItems[0].Remove();
}

 

清空ListView控件内所有项

listView1.Items.Clear();
posted @ 2010-07-03 09:44  小 .xin  阅读(1285)  评论(0编辑  收藏  举报
回到页首