使用C#将项目添加到列表视图的帮助 [英] Help with adding items to a listview using C#

查看:82
本文介绍了使用C#将项目添加到列表视图的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是一个初学者,只需要一些帮助即可解决此问题.

我认为它很简单,但已设法弄清楚了,但即时通讯无处可寻.
我目前有四个文本框,向它们添加数据,然后单击添加"以将其添加到列表视图.

我能够第一次添加,但是之后不能再次添加,这也不会从第一文本框中向第一列添加数据,而是将第一列留在第二列之外.


下面是代码

Hi,

I''m a beginner and just need some help solving this problem.

I think its simple but have tried to figure it out but im getting no where.
I currently have four text boxes to which I add data then click the add to add this to the listview.

I am able to add the first time but then can not add again also this does not add data from the first text box to the first column but leaves the first column out and starts from the second column.


below is the code

ListViewItem item1 = new ListViewItem();
// add items.
item1.SubItems.Add((Convert.ToString(StudentHouses.HouseNo)));
item1.SubItems.Add((StudentHouses.AddressLine1));
item1.SubItems.Add((StudentHouses.AddressLine2));
item1.SubItems.Add((StudentHouses.AddressLine3));
//Add the items to the ListView.
listView1.Items.Add( item1 );
this.Controls.Add(listView1);



任何帮助将不胜感激.
谢谢

[edit]主题:请勿大喊大叫.所有大写字母都被认为是在互联网上喊叫,并且粗鲁-OriginalGriff [/edit]



Any help would be appreciated.
thankyou

[edit]Subject: Don''t SHOUT. All uppercase is considered SHOUTING on the internet, and rude - OriginalGriff[/edit]

推荐答案

不知道为什么您不能再次添加,除非您尝试要添加重复的列表视图项,请尝试调用listView1.Items.Clear();

关于第二栏的开头,我也感到困惑.项目文本显示在第一列,子项目从第二列开始.这意味着您的代码应该更像:(还包括其他改编版)

Not sure why you wouldn''t be able to add again unless your trying to add a duplicate list view item, try calling listView1.Items.Clear();

With regards to the starting at second column it is confusing I found also. The item text appears in the first column and sub items start at column two. This means your code should be more like: (included other adaptations as well)

//Clear listView1 items.
listView1.Items.Clear();

//Create item1
ListViewItem item1 = new ListViewItem();

//Set Text which represents first column
item1.Text = Convert.ToString(StudentHouses.HouseNo);

//Add sub items starting with second column
item1.SubItems.Add((StudentHouses.AddressLine1));
item1.SubItems.Add((StudentHouses.AddressLine2));
item1.SubItems.Add((StudentHouses.AddressLine3));

//Add the items to the ListView.
listView1.Items.Add( item1 );

//Add listView1 control if it has not already been added
if(!this.Controls.Contains(listView1))
{
    this.Controls.Add(listView1);
}


免责声明:
此代码应OP请求按原样"发布.
它正在运行并且经过了很好的测试,但是我不能为任何目的对其价值负责.
这是演示应用程序中我未发表的文章 动态方法分派器" 的代码片段,与本文的主题无关.

我提前发布的唯一原因是OP要求它.

本文的目的是基于数据模型与UI的分离给出代码设计的思想,而不是详细解释每个声明.
Disclaimer:
This code posted "as is", on request by OP.
It is working and very well-tested but I cannot take responsibility for its value for the any purposes.
This is a code fragment from the demo application for my unpublished article "Dynamic Method Dispatcher" and is not related to the main topic of my article.

The only reason I post it in advance is that OP asked for it.

The purpose if this post is to give an idea of code design based on separation of data model from UI, not for explaining each declaration in any level of detail.
using System;
using System.Windows.Forms;
using Time = System.DateTime;

//...

ListViewItem ShowCommon(
        Time time, ListView listView,
        MessageInfo info,
        string customData, bool autoResize) {
    ListViewItem item =
        new ListViewItem(TestRenderers[(int)CommonColumn.Time](ref time, ref info, customData, true));
    for (int index = 0; index < listView.Columns.Count - 1; index++)
        item.SubItems.Add(new ListViewItem.ListViewSubItem());
    for (
        CommonColumn index = (CommonColumn)1;
        index < (CommonColumn)(Enum.GetValues(typeof(CommonColumn)).Length - 1);
        index++) {
            item.SubItems[(int)index].Text =
                TestRenderers[(int)index](ref time, ref info, customData, true);
    } //loop
    item.SubItems[listView.Columns.Count - 1].Text = customData;
    listView.Items.Add(item);
    if (autoResize)
        AutoResize(listView);
    listView.EnsureVisible(item.Index);
    ShowStatus(ref time, ref info, customData);
    return item;
} //ShowCommon



此代码表示Windows消息的通用表示.它透明地传递ListViewItem的实例,以添加特定于特定消息ID范围的数据,例如键盘,鼠标,状态等.

请注意,不使用立即数(0、1和null除外).所有数据都是从一些枚举类型定义和基于这些定义的数组中提取的.

关于枚举,请参阅我的三篇文章系列:
枚举类型不枚举!解决.NET和语言限制 [ ^ ]
人类可读的枚举元数据 [基于枚举的命令行实用程序 [



This code represents general purpose presentation of a Windows message. It transparently passes the instance of the ListViewItem for adding data specific to a specialized ranges of message IDs, like keyboard, mouse, status, etc.

Pay attention that no immediate constants are used (except 0, 1 and null). All data is extracted from some enumeration type definitions and arrays based on those definitions.

On enumerations, please see my series of three articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^]
Human-readable Enumeration Meta-data[^]
Enumeration-based Command Line Utility[^].

The most relevant is the first article in the series, and a bit of the second one. Even though my demo application (small part shown above) does not use any code from any of these three libraries, some ideas are shared.

—SA


这篇关于使用C#将项目添加到列表视图的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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