在ListView中添加多个列时出现问题(使用VS 2015) [英] Problem adding multiple columns in ListView (with VS 2015)

查看:98
本文介绍了在ListView中添加多个列时出现问题(使用VS 2015)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

在VS 2015的设计器中,我添加了具有多列的ListView,但是我没有找到有关如何填充此ListView的任何功能性解决方案.我在网上发现了许多潜在的解决方案,但没有一个起作用!

In the designer of VS 2015 I added a ListView with multiple columns, but I don't find any functional solution on how to fill this listView. I find many potential solutions on the net but none does work!

例如,当我尝试MS提出的建议时(https://msdn.microsoft.com/zh-cn/library/wh9a3t2x(v=vs.110).aspx?cs-save-lang=1&cs-lang = vb#code-snippet-1): ListView1.Items(0).SubItems.Add( "John Smith" );

For example when I try what MS proposes (https://msdn.microsoft.com/en-us/library/wh9a3t2x(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1): ListView1.Items(0).SubItems.Add("John Smith");

我得到:"类型名称'ListViewSubItem在类型'ListViewItem'中不存在'.

在其他地方,他们告诉我要做: var item1 = new ListViewItem(new [] {"id123","Tom","24"});

At other places they tell me to do: var item1 = new ListViewItem(new[] { "id123", "Tom", "24" });

尝试此操作时,我得到:'ListViewItem'不包含带有1个参数的构造函数"".

When I try this I get: "'ListViewItem' does not contain a constructor that takes 1 arguments".

我想.net发生了重大变化,没有地方有记载,有人有想法吗?

I suppose there has been a major change in .NET that is documented nowhere, anyone have an idea?

推荐答案

安德烈(Andre) Pletschette,

Hi Andre Pletschette,

谢谢您在这里发布.

对于您的问题,您可以尝试以下有关如何填充列表视图的代码.

For your question, you could try the following code about how to fill the listview.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace listview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            //Add column header
            listView1.Columns.Add("ProductName", 100);
            listView1.Columns.Add("Price", 70);
            listView1.Columns.Add("Quantity", 70);

            //Add items in the listview
            string[] arr = new string[4];
            ListViewItem itm;
            //Add first item
            arr[0] = "product_1";
            arr[1] = "100";
            arr[2] = "10";
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);

            //Add second item
            arr[0] = "product_2";
            arr[1] = "200";
            arr[2] = "20";
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);

        }
    }
}

这是输出.

我希望这会对您有所帮助.

I hope this would be helpful to you.

如果您对此问题还有其他疑问,请随时与我们联系.

If you have something else about this issue, please feel free to contact us.

最好的问候,

温迪


这篇关于在ListView中添加多个列时出现问题(使用VS 2015)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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