在列表视图中选择一行 [英] Select a row in listview

查看:48
本文介绍了在列表视图中选择一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web开发人员,致力于在WinForms中开发的项目的一部分.所以我的问题可能是一个基本的问题.尝试忍受它.

I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.

我的页面上有两个列表视图,并且两个按钮均适用于该按钮.

I have two list views on my page and a remove button that works for both.

问题.

  1. 我在运行程序时无法在两个列表视图中选择一行,可能是它需要一些属性吗?
  2. 如果我能够选择要检测选择了哪个列表视图项目的行,那我该怎么做?
  3. 我有三列,并使用下面的代码绑定了数据.

  1. I am not able to select a row in both the list view when I run my program, may be some property needed for it?
  2. If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
  3. I have three columns and have bound the data by using the code below.

    listView1.Columns.Add("ID",20);
    listView1.Columns.Add("Name",40);
    listView1.Columns.Add("Mobile",40);


    foreach (var item in dataList)
    {
        newItem = new ListViewItem();
        newItem.SubItems.Add(item.ID.ToString());
        newItem.SubItems.Add(item.Name);
        newItem.SubItems.Add(item.Mobile.ToString());
        listView1.Items.Add(newItem);   
    }

,但 ID 列留空,并且数据在这种意义上开始绑定.

but the ID column is left blank and the data starts to bind in these sense.

ID Name Mobile
   1    abc
   2    xyz

那我该如何正确显示数据?

So how do I properly show the data?

  1. 最后,我想使用我的 ID 列删除数据.因此,如果我给出 width = 0 ,这是隐藏列的最佳方法吗?
  1. Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?

推荐答案

  1. 请参见 ListView.FullRowSelect 财产.
  2. 请参见 ListView.SelectedItems 财产.请注意,默认情况下 ListView 允许多选.
  3. 通过构造函数设置项目文本: newItem = new ListViewItem(item.ID.ToString()); ,然后添加其余子项( item.ID 除外)
  4. 如果要删除该列,只需将其从columns集合中删除.
  1. See ListView.FullRowSelect property.
  2. See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
  3. Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
  4. If you want to delete the column, just remove it from the columns collection.

这篇关于在列表视图中选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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