简体   繁体   English

如何添加到特定的列表视图列

[英]How to add to specific listview column

Currently I'm just using listviewitem.SubItems.Add() to add items to my listview. 目前,我只是使用listviewitem.SubItems.Add()将项目添加到我的listview中。

The problem is that it only adds to the first 2 columns (Goes column by column). 问题在于它仅添加到前2列(逐列进行)。

I want to be able to add to any column, for example: skip the yymm, total trans and yyww columns and add to the amount in doc column. 我希望能够添加到任何列,例如:跳过yymm,trans和yyww总计列,并添加到doc列中的金额。

在此处输入图片说明

This is how I currently add to the listview: 这是我目前添加到列表视图的方式:

int totItems = Seq3.Count - 1;

if (PercentPopTolerance1.Count - 1 > totItems)
    totItems = PercentPopTolerance1.Count - 1;

for (int i = 0; i <= totItems; i++)
{
    ListViewItem lvi = new ListViewItem();
    string item1 = "";
    string item2 = "";

    if (Seq3.Count - 1 >= i)
        item1 = Seq3[i].ToString();

    if (PercentPopTolerance1.Count - 1 >= i)
        item2 = PercentPopTolerance1[i].ToString();

    lvi.SubItems.Add(item1);
    lvi.SubItems.Add(item2);

    listView2.Items.Add(lvi);
}

Just add an empty string to bypass unneeded columns: 只需添加一个空字符串即可绕过不需要的列:

lvi.SubItems.Add(item1);

lvi.SubItems.Add(string.Empty); // skip Percent column

lvi.SubItems.Add(item2);

I'd create a class to represent a row in the grid: 我将创建一个类来表示网格中的一行:

public class MyClass
{
    string SeqNum { get; set; }
    string Percent { get; set; }
    string YYMM { get; set; }
    string TotalTrans { get; set; }
    string YYWW { get; set; }
    string AmountInDoc { get; set; }
}

Then modify your code to create a list of those objects, insert only the values you need, and then attach the list to the grid. 然后修改您的代码以创建这些对象的列表,仅插入所需的值,然后将列表附加到网格。

(Note: This is all untested and you'll need to play with it to get it work in your situation.) (注意:这都是未经测试的,您需要使用它才能使它在您的情况下起作用。)

var myList = new List<MyClass>();

for (int i = 0; i <= totItems; i++)
{
    var myClass = new MyClass();

    if (Seq3.Count - 1 >= i)
        myClass.SeqNum = Seq3[i].ToString();

    if (PercentPopTolerance1.Count - 1 >= i)
        myClass.Percent = PercentPopTolerance1[i].ToString();

    myList.Add(myClass);
}

myGrid.ItemsSource = myList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM