• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

help with Listview...wont populate all columns

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
0
NewMom2Brandon
Asked:
NewMom2Brandon
  • 3
  • 2
1 Solution
 
Ravi SinghSenior Software EngineerCommented:
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(',')));
         }
0
 
NewMom2BrandonAuthor 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
0
 
Ravi SinghSenior Software EngineerCommented:
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)));
            }
         }
0
 
Ravi SinghSenior Software EngineerCommented:
Sorry, the 'else' part should contain:

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

(it had an extra bracket at the end)
0
 
NewMom2BrandonAuthor Commented:
Thank you Very much!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now