none
how to append items to existing listview RRS feed

  • Question

  • Hi

    i have listview with headrs in place (8 columns) the first  four of them get populated at the start of programm and do not need to be changed; and the remaining  are to be populated  periodically  by 10  background threads ( equal to number of rows in listview);

    void InitListCtr()
    {
        listView1.Columns.Add("DB Server Name", 80, HorizontalAlignment.Left);
        listView1.Columns.Add("Locale No ", 80, HorizontalAlignment.Left);
        listView1.Columns.Add("Db No", 65, HorizontalAlignment.Left);
        listView1.Columns.Add("Locale ", 85, HorizontalAlignment.Left);
        listView1.Columns.Add("Status ", 65, HorizontalAlignment.Left);
        listView1.Columns.Add("Service  ", 65, HorizontalAlignment.Left);
        listView1.Columns.Add("Records", 65, HorizontalAlignment.Left);
        listView1.Columns.Add("Next Run  ", 95, HorizontalAlignment.Left);
        
    }

    the first four columns are added like this

      for (int i = 0; i < 10; i++)
    {
                    tm = new string[5];
                    tm[0] = CDBProcessHandlerEx.SwitchObjects[i].dbserver;
                    tm[1] = CDBProcessHandlerEx.SwitchObjects[i].localNo.ToString();
                    tm[2] = CDBProcessHandlerEx.SwitchObjects[i].dbNo.ToString();
                    tm[3] = CDBProcessHandlerEx.SwitchObjects[i].localeName;
                itm = new ListViewItem(tm);
                    listView1.Items.Add(itm);
      }   

    my question is:

    assuming localNo is unique 

    assuming my threads return string[] object including localNo

    how can I append  the rest of columns to the existing columns?

    Saturday, June 1, 2013 1:29 PM

Answers

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
                InitListCtr();
            }
            void InitListCtr()
            {
                // Set the initial sorting type for the ListView.
                listView1.Sorting = SortOrder.None;
                // Disable automatic sorting to enable manual sorting.
                listView1.View = View.Details;
                // Set the view to show details.
                //listView1.View = View.Details;
                // Allow the user to edit item text.
                listView1.LabelEdit = false;
                // Allow the user to rearrange columns.
                listView1.AllowColumnReorder = false;
                // Select the item and subitems when selection is made.
                listView1.FullRowSelect = true;
                // Display grid lines.
                listView1.GridLines = true;
                // Sort the items in the list in ascending order.
                listView1.Columns.Add("DB Server Name", 80, HorizontalAlignment.Left);
                listView1.Columns.Add("Locale No ", 80, HorizontalAlignment.Left);
                listView1.Columns.Add("Db No", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Locale ", 85, HorizontalAlignment.Left);
                listView1.Columns.Add("Status ", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Service  ", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Records", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Next Run  ", 95, HorizontalAlignment.Left);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    ListViewItem NewRow = new ListViewItem(
                      new string[] { "Column A", "Column B", "Column C", "Column D",
                                      "","","",""},
                                      -1, Color.Empty, Color.LightSkyBlue, null);
                    listView1.Items.AddRange(new ListViewItem[] { NewRow });
                }
                for (int j = 0; j < 10; j++)
                {
                    listView1.Items[j].SubItems[4].Text = "Column E";
                    listView1.Items[j].SubItems[5].Text = "Column F";
                    listView1.Items[j].SubItems[6].Text = "Column G";
                    listView1.Items[j].SubItems[7].Text = "Column H";
                }
            }
        }
    }


    jdweng

    • Marked as answer by kobosh Sunday, June 2, 2013 10:16 PM
    Saturday, June 1, 2013 5:16 PM

All replies

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
                InitListCtr();
            }
            void InitListCtr()
            {
                // Set the initial sorting type for the ListView.
                listView1.Sorting = SortOrder.None;
                // Disable automatic sorting to enable manual sorting.
                listView1.View = View.Details;
                // Set the view to show details.
                //listView1.View = View.Details;
                // Allow the user to edit item text.
                listView1.LabelEdit = false;
                // Allow the user to rearrange columns.
                listView1.AllowColumnReorder = false;
                // Select the item and subitems when selection is made.
                listView1.FullRowSelect = true;
                // Display grid lines.
                listView1.GridLines = true;
                // Sort the items in the list in ascending order.
                listView1.Columns.Add("DB Server Name", 80, HorizontalAlignment.Left);
                listView1.Columns.Add("Locale No ", 80, HorizontalAlignment.Left);
                listView1.Columns.Add("Db No", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Locale ", 85, HorizontalAlignment.Left);
                listView1.Columns.Add("Status ", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Service  ", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Records", 65, HorizontalAlignment.Left);
                listView1.Columns.Add("Next Run  ", 95, HorizontalAlignment.Left);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    ListViewItem NewRow = new ListViewItem(
                      new string[] { "Column A", "Column B", "Column C", "Column D",
                                      "","","",""},
                                      -1, Color.Empty, Color.LightSkyBlue, null);
                    listView1.Items.AddRange(new ListViewItem[] { NewRow });
                }
                for (int j = 0; j < 10; j++)
                {
                    listView1.Items[j].SubItems[4].Text = "Column E";
                    listView1.Items[j].SubItems[5].Text = "Column F";
                    listView1.Items[j].SubItems[6].Text = "Column G";
                    listView1.Items[j].SubItems[7].Text = "Column H";
                }
            }
        }
    }


    jdweng

    • Marked as answer by kobosh Sunday, June 2, 2013 10:16 PM
    Saturday, June 1, 2013 5:16 PM
  • thanks but i get this error

    InvalidArgument=Value of '5' is not valid for 'index'.
    Parameter name: index

    Sunday, June 2, 2013 3:14 PM
  • Note : I added 4 blanks fields in the line below.

                   ListViewItem NewRow = new ListViewItem(
                     
    new string[] { "Column A", "Column B", "Column C", "Column D",
                                     
    "","","",""},
                                     
    -1, Color.Empty, Color.LightSkyBlue, null);
     


    jdweng

    Sunday, June 2, 2013 3:45 PM
  • still not working; this is going to add new rows at the end of existing rows  with four columns  A,B C,D

    what i want is to   insert   text   ( four) at the end of each  existing row  

    old1  old2    old3  old4  new1  new2  new3  new4

    Sunday, June 2, 2013 7:59 PM
  • Try my code in a new project and you will see that it works.  There are a fw minor differences between yhou r original code and mine that your may not of noticed.  first is the four blanks that I added.  Second I used AddRange (not Add) method.

    Try enabling gidLines (listView1.GridLines = true;) with your code and see if the data is going into seperate columns.  I didn't get the Listview to display properly until I used FullRowSelect.


    jdweng

    Sunday, June 2, 2013 8:08 PM
  • my code worked  when i made this change

    string[] tm=new string[8] instead of new string[5]

    in the list initialization step;

    Sunday, June 2, 2013 10:14 PM