Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ObjectListView显示包含当前行索引的列?_C#_.net_Winforms_Objectlistview - Fatal编程技术网

C# 如何使用ObjectListView显示包含当前行索引的列?

C# 如何使用ObjectListView显示包含当前行索引的列?,c#,.net,winforms,objectlistview,C#,.net,Winforms,Objectlistview,我有以下课程: public class people { public string name { get; set; } public string hobby { get; set; } } 我想显示如下列表: no | name | hobby ----------------------- 01 | kim | tv 02 | kate | pc 03 | kim | tv 04 | kate | pc objectListView1.FormatRow +=

我有以下课程:

public class people
{
    public string name { get; set; }
    public string hobby { get; set; }
}
我想显示如下列表:

no | name | hobby
-----------------------
01 | kim  | tv
02 | kate | pc
03 | kim  | tv
04 | kate | pc
objectListView1.FormatRow += delegate(object sender, FormatRowEventArgs args) {
    args.Item.Text = args.RowIndex.ToString();
};
我所知道的实现这一目标的唯一方法是将人们转变为阶级

public class people
{
    public string no{ get; set; }
    public string name { get; set; }
    public string hobby { get; set; }
}
并循环
List
设置每个
no
属性


是否有更好的方法添加索引号列?

是的,您可以通过如下方式处理“FormatRow”事件来实现所需的功能:

no | name | hobby
-----------------------
01 | kim  | tv
02 | kate | pc
03 | kim  | tv
04 | kate | pc
objectListView1.FormatRow += delegate(object sender, FormatRowEventArgs args) {
    args.Item.Text = args.RowIndex.ToString();
};
这将在ObjectListView的第一列上显示从零开始的索引。如果你想让它显示为01,02。。。可以指定格式字符串和偏移量,如:

args.Item.Text = (args.RowIndex + 1).ToString("D2");
作为旁注:使用AspectGetter不可能实现这一点(这将更加优雅),因为在调用它时,行尚未添加到OLV,因此没有索引信息

// !!! won't work !!!
indexColumn.AspectGetter += delegate(object rowObject) {
    return objectListView1.IndexOf(rowObject).ToString();
};

如果将来能有这个内置的,那就太好了!如果我的专栏不是第一篇呢?我使用图像列作为第一列,因此这不起作用,我使用的是e.Item.SubItems[1].Text=(e.DisplayIndex+1).ToString();但它很难看,因为我不应该使用子项,所以我将其更改为(e.Model作为MyModel);但无论我将什么设置为“否”,它都不会更新,我在setter中使用了NotifyPropertyChanged属性true和激发PropertyChanged事件,但什么都没有happens@ShinoLex:可能是从
FormatCell
事件内部?但我还没试过。