如何在单击事件按钮上将线串插入到列表视图中 [英] How to insert a line string into listview on click event button

查看:77
本文介绍了如何在单击事件按钮上将线串插入到列表视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

谁能帮助我如何在单击事件按钮上将线串插入到列表视图中.

基本上我的项目是基于餐厅的,如果我想在列表视图中添加 starter main 课程,我想在它们之间用行字符串将它们分开像这样的两个不同的课程:

Hello

Could anyone help me how to insert a line string into listview on click event button.

Basically my project is restaurant based, for instant if I want to add a starter and a main course into my listview, I want to divide them with a line string in between the two different courses like so:

1 Soup		£0.00		
1 Bruschetta	£0.00	
****** Course 1 ******
1 Ravioli	£0.00
1 Lasagne	£0.00
1 Veg. Pizza    £0.00



这是单击事件按钮的代码(请参见下文),但不显示任何内容.



This is the code of the click event button (see below) but its not displaying nothing.

private void cmdCourseSeparator_Click(object sender, EventArgs e)
        {
            string line1 = "════";
            string line2 = "══════ Course 1 ══════";
            string line3 = "══════";
            string line4 = "══════ Course 2 ══════";
            ListViewGroup liViGroup = new ListViewGroup();
            liViGroup.HeaderAlignment = System.Windows.Forms.HorizontalAlignment.Center;
            if (liViGroup.Header == "Starter")
            {
                liViGroup.Items.Add(line1);
                //foodItem.SubItems.Add(line2);
                //foodItem.SubItems.Add(line3);
                //foodItem.SubItems.Add(line3);
            }
            this.TableOrderListView.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
            liViGroup});
        }




有人可以帮助我,告诉我我想念的是什么...

亲切的问候


Roni




Could someone please help me and tell what am I missing...

Kind Regards


Roni

推荐答案

我可以建议其他方法吗?

从您在此处发布的内容中,当您尝试保持(保存/加载)数据时,试图在不同课程之间进行区分时会遇到麻烦.对我来说,最好使用listview显示数据和一个用于修改课程的备用界面,这对我来说是更好的选择.

这样一来,您可以在每个课程中保留一组项目,这些项目可以轻松排序,轻松保存.接下来,您将可以允许用户为课程创建自己的名称并添加任意数量的名称.

在此阶段,重新考虑将数据存储在内存中的方式至少会使您更好.您应该考虑数据绑定;如果保留数据集合,则可以使用集合动态构建项目列表,该集合将数据绑定到列表框或其他可绑定数据的控件.

干杯.
Could I suggest a different approach?

From what you''re posting here you''re going to run into trouble when trying to differentiate between courses when you try to persist (save/load) the data. It would make sense to me that you would be better off to use the listview to display the data and an alternate interface for modifying the courses.

This would allow you to keep a collection of items in each course, easily sortable, easily saved. Down the road, you would then be able to allow the user to create their own names for the courses and add as many as they like.

At this stage, you''re at least better off with reconsidering the way you store the data in memory. You should consider databinding; if you keep collections of data you will be able to construct the list of items on the fly with a collection that is then data bound to a listbox or other databindable control.

Cheers.


ListViewGroup.Header从未分配给它,因此在添加项的if语句中永远不会为true:
The ListViewGroup.Header is never assigned to so it will never be true in the if statement where you add items:
ListViewGroup liViGroup = new ListViewGroup();
liViGroup.HeaderAlignment = System.Windows.Forms.HorizontalAlignment.Center;

if (liViGroup.Header == "Starter") // No header was assigned to liViGroup, so it never enters this block
{
     liViGroup.Items.Add(line1);


这篇关于如何在单击事件按钮上将线串插入到列表视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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