winforms 列表视图元素右移

pgpifvop  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(126)

我正在创建一个包含14列的listview元素,看起来一切都被读入到了listview中,但是,它都向右移动了1列?

这是我的代码:

listView2.Items.Clear();
List<string> Event = new List<string>();
List<string> Currency = new List<string>();
List<string> Ammount = new List<string>();
List<string> DateAndTime = new List<string>();
List<string> customerName = new List<string>();
List<string> mpnumber = new List<string>();
List<string> Comment = new List<string>();
List<string> transactionid = new List<string>();
List<string> transferid = new List<string>();
List<string> paymentpoint = new List<string>();
List<string> myshopnumber = new List<string>();
List<string> Bankaccount = new List<string>();
List<string> Date = new List<string>();
List<string> time = new List<string>();

foreach (ListViewItem item in listView1.Items)
{
    Event.Add(item.SubItems[0].Text);
    Currency.Add(item.SubItems[1].Text);
    Ammount.Add(item.SubItems[2].Text);
    DateAndTime.Add(item.SubItems[3].Text);
    customerName.Add(item.SubItems[4].Text);
    mpnumber.Add(item.SubItems[5].Text);
    Comment.Add(item.SubItems[6].Text);
    transactionid.Add(item.SubItems[7].Text);
    transferid.Add(item.SubItems[8].Text);
    paymentpoint.Add(item.SubItems[9].Text);
    myshopnumber.Add(item.SubItems[10].Text);
    Bankaccount.Add(item.SubItems[11].Text);
    Date.Add(item.SubItems[12].Text);
    time.Add(item.SubItems[13].Text);
}

List<int> indexOfRandom = new List<int>();
var random = new Random();
var lower = 0;
var upper = Event.Count;

for (int k = 0; k < numberOfRandoms; k++)
{
    var rannum = random.Next(lower, upper);
    indexOfRandom.Add(rannum);
}
listView2.BeginUpdate();
for (int i = 0; i < numberOfRandoms; i++)
{
    ListViewItem randomrows = new ListViewItem();
    randomrows.SubItems.Add(Event[i]);
    randomrows.SubItems.Add(Currency[i]);
    randomrows.SubItems.Add(Ammount[i]);
    randomrows.SubItems.Add(DateAndTime[i]);
    randomrows.SubItems.Add(customerName[i]);
    randomrows.SubItems.Add(mpnumber[i]);
    randomrows.SubItems.Add(Comment[i]);
    randomrows.SubItems.Add(transactionid[i]);
    randomrows.SubItems.Add(transferid[i]);
    randomrows.SubItems.Add(paymentpoint[i]);
    randomrows.SubItems.Add(myshopnumber[i]);
    randomrows.SubItems.Add(Bankaccount[i]);
    randomrows.SubItems.Add(Date[i]);
    randomrows.SubItems.Add(time[i]);
    listView2.Items.Add(randomrows);
}
listView2.EndUpdate();

希望问题有意义,随时向我询问信息
我的listview1本质上是一个很长的事务列表,运行良好,但是当我将它转移到listview2时,它似乎将所有的事务1都移到了右边?

f45qwnt8

f45qwnt81#

答案是@hansPassant发的,内容是:“第一列用ListViewItem.Text属性填充,子项从第二列开始,所以需要ListViewItem randomrows = new ListViewItem(Event[i]);等等...”

相关问题