Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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});
        }



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

Kind Regards


Roni
Posted

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.
 
Share this answer
 
Comments
JOAT-MON 4-Jan-11 19:13pm    
@MrJames - agreed +5; Based on the questions he has asked, LAPEC is working through his program a piece at a time exploring a number of ways to accomplish his task. These issues will undoubtedly need to be considered.
The ListViewGroup.Header is never assigned to so it will never be true in the if statement where you add items:
C#
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);
 
Share this answer
 
Comments
LAPEC 4-Jan-11 17:31pm    
Hi JOAT

Is it possible to show me how to ammend the ListViewGroup.Header...

Thanks in advance

roni
JOAT-MON 4-Jan-11 18:33pm    
Given the way you have written it, you need to assign the Header property. Add this line before the 'if' block:
liViGroup.Header = "Starter";
This will always get you into the 'if' block where you are adding items.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900