Member Avatar for westsiderailway

HI again,

I would like to know how i would use a loop for the following code, thanks for read this.

   LV1.Columns.Add(ds.Tables(0).Columns(0).ColumnName, 25)
        LV1.Columns.Add(ds.Tables(0).Columns(1).ColumnName, 75)
        LV1.Columns.Add(ds.Tables(0).Columns(2).ColumnName, 75)
        LV1.Columns.Add(ds.Tables(0).Columns(3).ColumnName)
        LV1.Columns.Add(ds.Tables(0).Columns(4).ColumnName)
        LV1.Columns.Add(ds.Tables(0).Columns(5).ColumnName, 75)
        LV1.Columns.Add(ds.Tables(0).Columns(6).ColumnName, 75)
        LV1.Columns.Add(ds.Tables(0).Columns(7).ColumnName, 75)
        LV1.Columns.Add(ds.Tables(0).Columns(8).ColumnName, 75)

           LVitem.Text = dt.Rows(0).Item(0).ToString
        LVitem.SubItems.Add(dt.Rows(0).Item(1).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(2).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(3).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(4).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(5).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(6).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(7).ToString)
        LVitem.SubItems.Add(dt.Rows(0).Item(8).ToString)

i was thinking along the lines of something like this

For i As Integer = 0 To dt.Rows.Count - 1

next

not sure what to put in the middle.

The second block is easy since each statement is identical except for the item index. Therefore, in the loop have one statement and replace the item index with the loop variable(i).

The first block is a little more tricky. Basically follow the same idea and replace the column index with the loop variable. However you will need a conditional statement that checks if the loop variable is 3 or 4 to exclude the second parameter.

@westsiderailway : Your thinking is in right way. Make a nested loop through 1 to DataTable Rows Item Counts - 1 to capture the subitems.
The loop should be

For i As Integer = 0 To dt.Rows.Count - 1
    LVitem.Text = dt.Rows(i).Item(0).ToString
    For j as Integer = 1 to dt.Rows(i).Item.Count - 1
        LVitem.SubItems.Add(dt.Rows(i).Item(j).ToString)
    Next
Next

Hope it can help you.

Member Avatar for westsiderailway

@shark1: thanks for your reply,

VS is saying that there is an error in your code line 3

Error 1 Overload resolution failed because no accessible 'Item' accepts this number of arguments.

i tried this...

        For i As Integer = 0 To dt.Rows.Count - 1
            LVitem.Text = dt.Rows(i).Item(0).ToString
            For j As Integer = 1 To dt.Rows(i).ItemArray.Count - 1
                LVitem.SubItems.Add(dt.Rows(i).Item(j).ToString)
            Next
        Next

BUT, the listview is empty.?

Member Avatar for westsiderailway

I figured this bit out by myself...

For i As Integer = 0 To dt.Rows.Count - 6
            LV1.Columns.Add(ds.Tables(0).Columns(i).ColumnName, 75)
        Next

if I put the figure on line 1 to "-1" I get an cannot find column 9, So I started with -9 and when down untill I no longer got an error. :-)

Member Avatar for westsiderailway

Have tried this...

        For l As Integer = 0 To dt.Rows.Count - 1
            For j As Integer = 1 To dt.Rows(0).Table.Rows.Count - 6
                LVitem.SubItems.Add(dt.Rows(l).Item(j).ToString)

            Next
        Next

it works,but only displays 1 line.

Member Avatar for westsiderailway

have also tried this....

 For l As Integer = 0 To dt.Rows.Count - 1
            For m As Integer = 1 To dt.Columns.Count - 1
                LVitem.SubItems.Add(dt.Rows(l).Item(m).ToString)
            Next
        Next

BUT, this also only displays 1 line.

Member Avatar for westsiderailway

I belive that i have figured some of it out...

listview1.items.add

add ROWS

lvitem.subitems.add

add Columns

  For l As Integer = 1 To dt.Rows.Count - 6
            For m As Integer = 1 To dt.Columns.Count - 6
                LV1.Items.Add(dt.Rows(l).Item(m).ToString)
                LVitem.SubItems.Add(dt.Rows(l).Item(m).ToString)
            Next

        Next

the above code add everything in the first column by rows, all i got to do is find out , how to make fill the columns as well. :-)

@westsiderailway : Sorry for my mistake. I was not present infront of VS IDE to check my codes at the time of answare. just I wanted to give you a way to proceed.
In First loop add the listviewitem to the listview object and in nested loop add the subitems. The loops should be

For l As Integer = 0 To dt.Rows.Count - 6
    'Declare listviewitem variable 
    'and add the item to the listview object
    Dim LVitem As New ListViewItem
    LVitem=LV1.Items.Add(dt.Rows(l).Item(0).ToString)

    For m As Integer = 1 To dt.Columns.Count - 6
        'Add the subitems
        LVitem.SubItems.Add(dt.Rows(l).Item(m).ToString)
    Next
Next

Hope it can give you a way.

Member Avatar for westsiderailway

Thanks very much for your guidance . :-)

works like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.