Results 1 to 8 of 8

Thread: [1.0/1.1] Adding items to Columns

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink [1.0/1.1] Adding items to Columns

    Hi all,

    I've created two columns on the list view as follows.

    Code:
    			ColumnHeader colHed;
    
    		// Header one
    			colHed = new ColumnHeader();
    			colHed.Text = "Name";
    			colHed.Width = 100;
    			this.lwDetails.Columns.Add(colHed);
    
    		// Header two
    			colHed = new ColumnHeader();
    			colHed.Text = "Address";
    			colHed.Width = 200;
    			this.lwDetails.Columns.Add(colHed);
    Then on a separate function I try to add data to those columns. Name and address of a one person store in a string array named items. I try to add data as follows.

    Code:
    			this.lwDetails.Items.Add(items[0]);
    			this.lwDetails.Items.Add(items[1]);
    lwDetails is my list view. In this case name and address are added to the same column. How can I correct it.

    Thanks,
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/1.1] Adding items to Columns

    Each item occupies a row in the ListView. If you want to fill more columns on a row then you have to add subitems to that item.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/1.1] Adding items to Columns

    Use a ListViewItem object to add your SubItems.
    C# Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3.     ColumnHeader colHed;
    4.  
    5.     // Header one
    6.     colHed = new ColumnHeader();
    7.     colHed.Text = "Name";
    8.     colHed.Width = 100;
    9.     this.lwDetails.Columns.Add(colHed);
    10.  
    11.     // Header two
    12.     colHed = new ColumnHeader();
    13.     colHed.Text = "Address";
    14.     colHed.Width = 200;
    15.     this.lwDetails.Columns.Add(colHed);
    16.  
    17.     // Setup Listivew stuff
    18.     this.lwDetails.View = View.Details;
    19.     this.lwDetails.FullRowSelect = true;
    20.     this.lwDetails.HideSelection = false;
    21.     this.lwDetails.MultiSelect = false;
    22.  
    23.     // load some data
    24.     ListViewItem lvwItem;
    25.     lvwItem =this.lwDetails.Items.Add(new ListViewItem("Test1"));
    26.     lvwItem.SubItems.Add("SubItem1");
    27.     lvwItem = this.lwDetails.Items.Add("Test2");
    28.     lvwItem.SubItems.Add("SubItem2");
    29.  
    30. }
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: [1.0/1.1] Adding items to Columns

    Code:
    // load some data
        ListViewItem lvwItem;
        lvwItem =this.lwDetails.Items.Add(new ListViewItem("Test1"));
        lvwItem.SubItems.Add("SubItem1");
        lvwItem = this.lwDetails.Items.Add("Test2");
        lvwItem.SubItems.Add("SubItem2");
    I'm not clear what happened on this code, I mean how to find the "Name" column to add the relevant data to it. And "Address" column also. Can you explain it little more.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/1.1] Adding items to Columns

    "SubItem1" and "SubItem2" are the text of the subitems being added.

    By setting the lvwItem object to the resulting .Add of the new listviewitem we get a object reference set to that exact item/row. Using the object variable we can access the properties and methods of the row. .SubItems.Add is the method for adding new subitems/text to the other secondary columns.
    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: [1.0/1.1] Adding items to Columns

    Oki, I got the point. And also I no need to set the Details property of the ListView, since I can set it on the Properties dialog box. http://www.vbforums.com/showthread.p...96#post3108596
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/1.1] Adding items to Columns

    Yes that is true but I was doing it for a demo and as its my preference to code in the setup of the controls instead of property wwindow property setting changes. Explicit code setup is just a matter of personal preference.

    That link goes back to your original post?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: [1.0/1.1] Adding items to Columns

    Thanks Rob, I got the point.

    And sorry, that link put by mistake.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width