如何阅读多列ListView控件的列名? [英] How to read column names of a multicolumn ListView control?

查看:197
本文介绍了如何阅读多列ListView控件的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是找到一个ListView的列?

的名称的最佳途径

我转换成一个DataTable添加到列表使用一个程序我在这个论坛上找到,但我不能让它先放Id列,尤其是因为不是所有的数据表中有一栏身份证。

我可以收集listView.Columns.ToString搜索(),但我看到的格式为:

的columnHeader文字:ID

我要解析,以找到合适的名字ID。
这看起来并不像C#的精神。

我也试过: listView.SelectedItems [0] .SubItems [ID]
但是,这并不编译。


确定这里是完整的code。
确切的问题是,用户选择与快递名称和ID在ListView一排,但它也可能是编号和名称,顺序。发现所选择的快递ID的最快方法是:

  ListViewItem的SI = listCouriers.SelectedItems [0];
CourierId = si.SubItems [ID]文本。

但是,这并不工作。硬codeD的方法是这样的,但我不能保证某一天错误的列将被使用:

  ListViewItem的SI = listCouriers.SelectedItems [0];
CourierId = si.SubItems [1]。文本;

使用@HuorSwords方法导致这个不那么简单的解决方案,这对我的作品,而是取决于合理的假设列在 Col​​umnHeaderCollection 对应的顺序在表格上显示:

  ListViewItem的SI = listCouriers.SelectedItems [0];
字符串CourierId = NULL;
INT ICOL = 0;
的foreach(在listCouriers.Columns的columnHeader头​​)
{
    如果(header.Text ==ID)
    {
        CourierId = si.SubItems [ICOL]。文本;
        打破;
    }
    ICOL ++;
}


解决方案

由于 listView.Columns 的类型为 ListView.ColumnHeaderCollection ,那么它包含的columnHeader 的对象。

Col​​umnHeader.Text 包含列标题,这样你就可以检查混凝土柱:

 的foreach(在listView.Columns的columnHeader头​​)
{
      如果(header.Text ==ID)
      {
           // 做一点事...
      }
}

我不知道是否是最好的方法,但你并不需要解析的结果中找到ID值...

更新

此外,你试过用字符串索引引用它? > listView.Columns [ID]

What is the best way to find the names of the columns of a ListView?

I converted a DataTable to a List using a procedure I found on this forum, but I cannot make it to put the Id column first, especially because not all of my DataTables have a column "Id".

I can search in collection listView.Columns.ToString() but the format I am seeing is:

"ColumnHeader: Text: Id"

which I have to parse to find the proper name "Id". This does not look like the spirit of C#.

I also tried: listView.SelectedItems[0].SubItems["Id"] but that does not compile.


Ok Here is the complete code. The exact problem is that the user selects a row in the listView with Courier Names and Ids, but it could also be Ids and Names, in that order. The fastest way to find the Id of the selected courier would be:

ListViewItem si = listCouriers.SelectedItems[0];
CourierId = si.SubItems["Id"].Text;

but that does not work. The hardcoded way would be this, but I cannot guarantee that some day the wrong column will be used:

ListViewItem si = listCouriers.SelectedItems[0];
CourierId = si.SubItems[1].Text;

Using @HuorSwords method leads to this not-so-simple solution, which works for me, but depends on the reasonable assumption that the order of columns in the ColumnHeaderCollection corresponds to the display on the form:

ListViewItem si = listCouriers.SelectedItems[0];
string CourierId = null;
int icol = 0;
foreach (ColumnHeader header in listCouriers.Columns)
{
    if (header.Text == "Id")
    {
        CourierId = si.SubItems[icol].Text;
        break;
    }
    icol++;
}

解决方案

As listView.Columns is of type ListView.ColumnHeaderCollection, then it contains ColumnHeader objects.

The ColumnHeader.Text contains the column title, so you can check for concrete column with:

foreach (ColumnHeader header in listView.Columns)
{
      if (header.Text == "Id")
      {
           // Do something...
      }
}

I don't know if is the best approach, but you don't need to parse the results to find "Id" value...

UPDATE

Also, have you tried to reference it with the String indexer? > listView.Columns["Id"]

这篇关于如何阅读多列ListView控件的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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