We help IT Professionals succeed at work.

help with Listview...wont populate all columns

NewMom2Brandon
on
Medium Priority
221 Views
Last Modified: 2010-04-16
Ok I have a question regarding Listview

I have this function on form1 so that I can pass what is in the listview on form1 to form2

         // Populate the listbox configuration on the main screen
         foreach(ListViewItem CurrentItem in listviewConfig.Items )
         {                    
            string EntireRow = "";

            for (int i=0 ; i<CurrentItem.SubItems.Count ;i++)
            {
               EntireRow = EntireRow + CurrentItem.SubItems[i].Text + ", ";
               m_RootConList.Items.Add(EntireRow);
            }          
         }

Once I am at form 2  I have the same function to populate the second listview. Everything is currently going into the first column. How do I break it back out so it goes into the columns it is supposed to.

         foreach (ListViewItem item in rootConfig.Items)
         {
            string EntireRow = "";

            for (int i=0 ; i<item.SubItems.Count ;i++)
            {
               EntireRow = EntireRow + item.SubItems[i].Text + ", ";  
            }  
            listViewConn.Items.Add(EntireRow);
         }

here is an example of what I am getting in column 1.

The black cat ran across the street

what I need it to look like is

clolumn1                column2                  column3
The                       black                        cat ran across the street
Comment
Watch Question

Ravi SinghSenior Software Engineer

Commented:
Hi, you could modify your existing foreach loop on form 2 to:

         foreach (ListViewItem item in rootConfig.Items)
         {
            string EntireRow = "";

            for (int i=0 ; i<item.SubItems.Count ;i++)
            {
               EntireRow = EntireRow + item.SubItems[i].Text + ", ";  
            }  
            listViewConn.Items.Add(new ListViewItem(EntireRow.Split(',')));
         }

Author

Commented:
That works for the first two but the problem is that the third column contains multiple items separated by a ","

So is there a way to only have the first two items split then add the rest to the last column as is.

I hope this makes sense

clolumn1                column2                  column3
The                       black                        cat, ran, across, the, street
Senior Software Engineer
Commented:
Hi, try:


         foreach (ListViewItem item in rootConfig.Items)
         {
            string EntireRow = "";

            for (int i=0 ; i<item.SubItems.Count ;i++)
            {
               EntireRow = EntireRow + item.SubItems[i].Text + ", ";  
            }  

            string[] newItemArray = EntireRow.Split(',');

            if (newItemArray.Length > 3)
            {
                string thirdColumnText = string.Empty;
            for (int i = 2; i < newItemArray.Length; i++)
                  thirdColumnText += newItemArray[i] + " ";

            listViewConn.Items.Add(new ListViewItem(new string[] { newItemArray[0], newItemArray[1], thirdColumnText } ));

            }
            else
            {
                    listViewConn.Items.Add(new ListViewItem(newItemArray)));
            }
         }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Ravi SinghSenior Software Engineer

Commented:
Sorry, the 'else' part should contain:

 listViewConn.Items.Add(new ListViewItem(newItemArray));

(it had an extra bracket at the end)

Author

Commented:
Thank you Very much!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.