EFileTahi-A Posted July 26, 2005 Posted July 26, 2005 I have a listview set to viewmode = details with 4 columns defined. I just need to know how can I add text to a specific column, let's say column number 2? Quote
rot13 Posted July 26, 2005 Posted July 26, 2005 it's the second item in that row, or something like that... i'll check and get back to ya Quote
3xodus Posted July 26, 2005 Posted July 26, 2005 Hi, This is C#, but the idea is of course the same for VB .NET or whatever language you use: listView1.Items.Add("Item 1").SubItems.Add("SubItem 1"); Will add "Item 1" to the first column, and "SubItem 1" to the second column. You can add .SubItems.Add.. etc etc for as many columns as you have :) Quote Using: Visual Studio 2005/08 Languages: C#, Win32 C++, Java, PHP
EFileTahi-A Posted July 27, 2005 Author Posted July 27, 2005 (edited) Thank you very much folks! :) But what if I just want to add a item at column 2 instead of add values to columns 1 and 2? Because I only have access to subitems properties after adding to the first item too... Anyway, If I want to add items to 3 or more columns do I really need to do this? for (int i = 0; i < 2000; i++) { //Adds items to the first and second columns this.listView1.Items.Add("bla" + i).SubItems.Add("bla" + i); //This adds an item to the third column this.listView1.Items[i].SubItems.Add("bla" + i); //This adds an item to the forth column this.listView1.Items[i].SubItems.Add("bla" + i); // etc... } Edited July 27, 2005 by EFileTahi-A Quote
3xodus Posted July 27, 2005 Posted July 27, 2005 But what if I just want to add a item at column 2 instead of add values to columns 1 and 2? Because I only have access to subitems properties after adding to the first item too... If you just want to access for example the second column, you can do something like: listView1.Items[0].SubItems[1].Text = "Whatever"; or depending on your situation, you could use the Key, which you would add at the same time as adding the item. Then to access the second column you could do something like: listView1.Items["ItemKey"].SubItems[1].Text = "New Text"; I'm currently writing a UI for an online gaming service, and I'm showing the users ping next to their username in a listView. Whenever I add a user to the listview, I also add their username as the key, which makes it easy for me to update their ping (and other details) whenever I want, by doing listView.Items[string UserName].SubItems.blah... Hope this makes sense :) Quote Using: Visual Studio 2005/08 Languages: C#, Win32 C++, Java, PHP
EFileTahi-A Posted July 27, 2005 Author Posted July 27, 2005 lol, I found it by the time I las posted.... But tks anyway... Now am trying to select an item through code, but I just can't! Why there is no simple option like listView1.SelectIndex = 0??? Quote
pendragon Posted July 27, 2005 Posted July 27, 2005 If I remember correctly you can do the following this.ListView1.Items[3].Selected = true; Quote
Wile Posted July 27, 2005 Posted July 27, 2005 lol, I found it by the time I las posted.... But tks anyway... Now am trying to select an item through code, but I just can't! Why there is no simple option like listView1.SelectIndex = 0??? Because multiple items can be selected ;) (Do check for this in your code!!) pendragon got the solution to your question though. Quote Nothing is as illusive as 'the last bug'.
EFileTahi-A Posted July 28, 2005 Author Posted July 28, 2005 Ok... I guess you buddies solved all my probs... for now ;) Thank you all! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.