Hi all,

I have an ArrayList that contains 70 items. I would like to display these items in Listview. Therefore, I used the following code. However, When I press the button, program will only display the first seven items in the arrayList. The rest of items are not being displayed. How can I fix this problem ?

Cheers,

private void btn_CreateReport_Click(object sender, EventArgs e)
        {
            
            lstview.Items.Clear();
            int counterOfArraylist = mcarraylist.Count;
            string[] str = new string[counterOfArraylist];
            for (int i = 0; i < str.Length; i++)
            {

                str[i] = mcarraylist[i].ToString();
                
            }
            lstview.Items.Add(new ListViewItem(str));                                                                                                                                                    
        }

Recommended Answers

Hi all,

I have an ArrayList that contains 70 items. I would like to display these items in Listview. Therefore, I used the following code. However, When I press the button, program will only display the first seven items in the arrayList. The rest of items are not …

Jump to Post

Thanks for your response,

It display all items. that is great. However, Listview has 7 column. That is why, I would like to display each item under this column. The above code display the items by using only one column (First column). What I can do?

Cheers,

Jump to Post

All 7 Replies

Hi all,

I have an ArrayList that contains 70 items. I would like to display these items in Listview. Therefore, I used the following code. However, When I press the button, program will only display the first seven items in the arrayList. The rest of items are not being displayed. How can I fix this problem ?

Cheers,

private void btn_CreateReport_Click(object sender, EventArgs e)
        {
            
            lstview.Items.Clear();
            int counterOfArraylist = mcarraylist.Count;
            string[] str = new string[counterOfArraylist];
            for (int i = 0; i < str.Length; i++)
            {

                str[i] = mcarraylist[i].ToString();
                
            }
            lstview.Items.Add(new ListViewItem(str));                                                                                                                                                    
        }

ArrayList are pretty much deprecated, you should use List<type>.
mclist being defined as following

List<string> mclist = new List<string>();
private void btn_CreateReport_Click(object sender, EventArgs e)
        {
            
            lstview.Items.Clear();

            foreach(string elem in mclist)
            {
                   lstview.Items.Add(new ListViewItem(elem));           
            }
                                                                                                                                             
        }

I didn't test it out, but if I would do this it would look like this. I am assuming that the listview item can take a string as parameter.

You could also create new ListViewItem and change his properties and than add it to the listview.

Thanks for your response,

It display all items. that is great. However, Listview has 7 column. That is why, I would like to display each item under this column. The above code display the items by using only one column (First column). What I can do?

Cheers,

Thanks for your response,

It display all items. that is great. However, Listview has 7 column. That is why, I would like to display each item under this column. The above code display the items by using only one column (First column). What I can do?

Cheers,

You want the information to be split between columns ? I am really not sure of what you mean, if you can post screenshot, it'll be awesome.

In this case you'd need to have another class that could be called customers that would have all the properties needed (Address,name, date,etc)

Then you can use a list List<Customer> list = new List<Customer>();

Foreach would look like this

foreach(Customer cust in list)
{
      //here you can take a look at each property of the object
     //if doing cust.propertyName (which could be address, name,etc 
    // in this case. Then you can assign each property of the object //to the appropriate column. 
}

Just separate with coma like this:

lstview.Items.Add(new ListViewItem(cust.Name), new ListViewItem(cust.Adress));

Error 3 The best overloaded method match for 'System.Windows.Forms.ListView.ListViewItemCollection.Add(string, string)' has some invalid arguments

Try replacing str.Length with counterOfArraylist. This is because you would like your loop to go through all the items in mcarraylist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.