Question Updating Rows in a Column (ListView)

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hey everyone, I currently have 30 items in my listview box. What I'd like to do now is go in and update the 'time' column and set the same time for each 10 rows throughout the entire list..

The first 10 rows should show "1:00"
The second 10 rows should show "1:01"
The third 10 rows should show "1:02"

Any help with this would be appreciated!
 
I would hope that you already know how to use a For loop, so you can use one to go through the Items of the ListView. The loop counter will get you the current item and you can then index its SubItems by the appropriate column index. You can then set the Text property of that subitem to the text you want.

To get that text, you can construct a String from a value based on the loop counter. You can use the integer division operator (\) to get that last number and then concatenate it to the appropriate String.

Before you ask me to write the code for you, you try writing it for yourself. You can simply follow the instructions I've provided and use the MSDN documentation or a web search to find information on anything you don't understand. If you have a good go at it and can't get it to work, post back what you've done and I'll be happy to help you fix it for yourself.

Also, make sure that you do it in stages. Each stage is simple so it's much easier to get each stage done than trying to do the whole thing in one go and getting in a tangle. For instance, start by writing a For loop that can access each item. Next, access the appropriate subitem inside the loop. Next, set the Text of that subitem to a constant value. Finally, set the Text to the actual desired value. Don't move onto any step until you have the previous one working. That way, if something doesn't work at any stage you will know exactly what small part of the code it is in because you know everything else before worked and you can always roll back to that last working state.
 
Hey jmcil..thanks for your response. I've been messing with it for a while and the more I try and think about it the more I find myself getting caught up with loops inside of loops with counters all over the place (inside my head)..

After reading your post I gave it a little more thought..This is what I've come up with so far.

VB.NET:
        Dim timecnt As DateTime = "1:00:00 PM"
        Dim timecnt2 As Integer = 0
        For x As Integer = 0 To ListView1.Items.Count - 1
            ListView1.Items(x).SubItems(2).Text = timecnt
            ListView1.Refresh()
        Next

This appears to be working..Except for a few things. It inputs the same time (1:00:00 PM) in all rows. This is expected as there is nothing in there right now to change that after every 10 rows. I guess I'll need another loop for this now?

I'm going to continue working with it, any help/advice would be appreciated!
 
Last edited:
You've done most of what I suggested there, which is good. I would get rid of that call to Refresh though. The loop will actually run very fast and you're just slowing it down by redrawing the ListView over and over unnecessarily.

All that's left is to calculate the offset and construct the String. Like I said, you can calculate the offset using integer division. It works exactly like regular division except it returns a whole number, so if you divide and number from 0 to 9 by 10 then the result will be 0, which is the offset you want for the items at those indexes. Etc. You can construct the String in various ways. One would be to create a Date that represents one o'clock and then AddMinutes on that inside the loop, which will add the result of your division as minutes to that date/time, and then call ToString and specify your format.
 
Back
Top