Search code examples
c#winformsobjectlistview

C# Add item to ObjectListView


My program extracts Windows Updates, detects the version numbers and logs them to columns (KB, Version) in a list view, but I'm trying to change this to an ObjectListView so I can sort the columns. I can't for the life of me work out how to write the results to an ObjectListView and nothing I try seems to work. Here's my current code:

foreach (string file in msu)
{
    string KB = GetKBNumber(file);
    Expand.MSU(file, TempDirectory + "\\" + KB);
    List<string> versions = GetVersionNumbers(TempDirectory + "\\" + KB);

    foreach (string version in versions)
    {
        ListViewItem itm = new ListViewItem(new[] { KB, version });
        olvOutput.Items.Add(itm);
    }
    PerformStep();
}

But it just writes blank data to the control. What am I doing wrong? Thanks in advance.

Edit: Here's the olvOutput designer code:

        // 
        // olvOutput
        // 
        this.olvOutput.AllColumns.Add(this.olvKBNumber);
        this.olvOutput.AllColumns.Add(this.olvVersion);
        this.olvOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.olvOutput.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.olvKBNumber,
        this.olvVersion});
        this.olvOutput.Location = new System.Drawing.Point(18, 12);
        this.olvOutput.Name = "olvOutput";
        this.olvOutput.ShowGroups = false;
        this.olvOutput.Size = new System.Drawing.Size(571, 193);
        this.olvOutput.TabIndex = 8;
        this.olvOutput.UseAlternatingBackColors = true;
        this.olvOutput.UseCompatibleStateImageBehavior = false;
        this.olvOutput.View = System.Windows.Forms.View.Details;
        // 
        // olvKBNumber
        // 
        this.olvKBNumber.AspectName = "";
        this.olvKBNumber.CellPadding = null;
        this.olvKBNumber.MaximumWidth = 100;
        this.olvKBNumber.MinimumWidth = 100;
        this.olvKBNumber.Text = "KB Number";
        this.olvKBNumber.Width = 100;
        // 
        // olvVersion
        // 
        this.olvVersion.AspectName = "";
        this.olvVersion.CellPadding = null;
        this.olvVersion.Text = "Version";
        this.olvVersion.Width = 113;

Solution

  • Modify the first of your snippets as:

    foreach (string file in msu)
    {
        string KB = GetKBNumber(file);
        Expand.MSU(file, TempDirectory + "\\" + KB);
        List<string> versions = GetVersionNumbers(TempDirectory + "\\" + KB);
    
        foreach (string version in versions)
        {
            olvOutput.AddObject(new { kbAspectName = KB, versionAspectName = version });
        }
        PerformStep();
    }
    

    ... and modify the second code snippet as:

    // 
    // olvKBNumber
    // 
    this.olvKBNumber.AspectName = "kbAspectName";
    
    // ...
    
    // 
    // olvVersion
    // 
    this.olvVersion.AspectName = "versionAspectName";
    

    Disclaimer: never worked with ObjectListView before so I am not saying this is the best way to achieve what you want.