0
votes

Hi I followed the answers to populating the listview, but the list view is populated horizontally but rather i need it to be populated column wise here's my code

listView1.Items.Clear();
listView1.Columns.Clear();

Dictionary<string, List<string>> Values = new Dictionary<string, List<string>>();
Values.Add("CODE", list_p_code);
Values.Add("NAME", list_p_name);
Values.Add("MRP", list_mrp);
Values.Add("ORDERS", list_order_qty);
Values.Add("PENDING", list_variance_qty);

foreach (string key in Values.Keys)
{
    listView1.Columns.Add(key, -2,HorizontalAlignment.Left);
    // Fill item
    ListViewItem item = new ListViewItem(key);

    // Fill Sub Items
    List<string> list = Values[key];

    foreach (string data in list)
    {
        item.SubItems.Add(data.ToString());
    }

    // Add to the ListView
    listView1.Items.Add(item);

}

The output is as follows:

CODE 1 2
NAME x y
ORDERS 4 4
...

but I want it as

CODE NAME ORDERS ...
1    x    4
2    y    4

How do I do it?

3
Take a look at this, it may be of use: stackoverflow.com/questions/15297007/… - Philip Gullick

3 Answers

0
votes

It's hard to understand what are you trying to do. If you want:

CODE NAME ORDERS ...
1    x    4
2    y    4

Then it would be natural to think, that you have a set of items with these 3 fields, and each item-code has respective name & orders-count.

Now, your data-structure is designed absolutely incorrect! That is why you have problems!

Let's look at your code:

  1. What if list_p_code.Length != list_p_name.Length ? How do you anticipate your listView to display that ?
  2. You are adding columns dynamically in the same cycle that actually adds list items. That is new columns appear in each iteration. But how do you anticipate adding an item before all columns are initialised? Think about this.

Example with correct data-structes. Note, that you should init all colums before you start iterating over items.

public class YourItem
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string MRP { get; set; }
    public int OrdersCount { get; set; }
    public int PendingCount { get; set; }           
}

void YourHandler()
{
    listView1.Items.Clear();
    listView1.Columns.Clear();

    var items = new[] 
    {
        new YourItem { Code = "Code1", Name = "Name1", MRP = "MRP1", OrdersCount = 1, PendingCount = 1 },
        new YourItem { Code = "Code2", Name = "Name2", MRP = "MRP2", OrdersCount = 2, PendingCount = 2 },
        new YourItem { Code = "Code3", Name = "Name3", MRP = "MRP3", OrdersCount = 3, PendingCount = 3 },
    };

    listView1.Columns.Add("CODE", -2,HorizontalAlignment.Left);
    listView1.Columns.Add("NAME", -2,HorizontalAlignment.Left);
    listView1.Columns.Add("MRP", -2,HorizontalAlignment.Left);
    listView1.Columns.Add("ORDERS", -2,HorizontalAlignment.Left);
    listView1.Columns.Add("PENDING", -2,HorizontalAlignment.Left);

    foreach (var item in items)
    {
        // Fill item
        ListViewItem item = new ListViewItem(item.Code);
        item.SubItems.Add(item.Name);
        item.SubItems.Add(item.MRP);
        item.SubItems.Add(item.OrdersCount);
        item.SubItems.Add(item.PendingCount);

        // Add to the ListView
        listView1.Items.Add(item);  
    }
}
0
votes

Add CODE, NAME, ORDERS as column headers, not part of the rows. Then add the {1,x,4} & {2,y,4} as the row data as you did before.

listview.Columns.Add()
0
votes

Example from MSDN: this is how you add (items and subitems, then the next item with its subitems):

// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("CODE",0);
item1.SubItems.Add("NAME");
item1.SubItems.Add("ORDERS");
ListViewItem item2 = new ListViewItem("1",1);
item2.SubItems.Add("x");
item2.SubItems.Add("4");
ListViewItem item3 = new ListViewItem("2",2);
item3.SubItems.Add("y");
item3.SubItems.Add("4");

Based on: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.items.aspx