Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to re write some of my code to make it a bit neater and a bit more efficient as I have just finished my proof of concept stage but I am struggling with writing the loop for the following bit of code.

VB
    Public Sub PopulateDelay()

'Code to check the settings page to see how many delays are to be loaded To be added in the future
'Code should run in a loop for the number of delays as per the settings page or until all delays are written to Dgvdelays

        'Clear All Rows
        FrmDas.DgvDelays.Rows.Clear()

        Dim ef As New ExcelFile
        ' Load Excel file.
        ef.LoadXls(FrmAdmin.TextBox1.Text)
        ' Select the first worksheet from the file.
        Dim ws As ExcelWorksheet = ef.Worksheets(0)

        Dim item As New DataGridViewRow
        Dim item2 As New DataGridViewRow
        Dim item3 As New DataGridViewRow
        Dim item4 As New DataGridViewRow
        Dim item5 As New DataGridViewRow
        item.CreateCells(FrmDas.DgvDelays)
        With item
            .Cells(1).Value = (ws.Cells("b2").Value.ToString())
            .Cells(2).Value = (ws.Cells("c2").Value.ToString())
            .Cells(3).Value = (ws.Cells("D2").Value.ToString())
            .Cells(4).Value = (ws.Cells("E2").Value.ToString())
            .Cells(5).Value = (ws.Cells("F2").Value.ToString())
        End With
        '---add the row---
        FrmDas.DgvDelays.Rows.Add(item)

        item2.CreateCells(FrmDas.DgvDelays)
        With item2
            .Cells(1).Value = (ws.Cells("b3").Value.ToString())
            .Cells(2).Value = (ws.Cells("c3").Value.ToString())
            .Cells(3).Value = (ws.Cells("D3").Value.ToString())
            .Cells(4).Value = (ws.Cells("E3").Value.ToString())
            .Cells(5).Value = (ws.Cells("F3").Value.ToString())
        End With
        '---add the row---
        FrmDas.DgvDelays.Rows.Add(item2)

        item3.CreateCells(FrmDas.DgvDelays)
        With item3
            .Cells(1).Value = (ws.Cells("b4").Value.ToString())
            .Cells(2).Value = (ws.Cells("c4").Value.ToString())
            .Cells(3).Value = (ws.Cells("D4").Value.ToString())
            .Cells(4).Value = (ws.Cells("E4").Value.ToString())
            .Cells(5).Value = (ws.Cells("F4").Value.ToString())
        End With
        '---add the row---
        FrmDas.DgvDelays.Rows.Add(item3)

        item4.CreateCells(FrmDas.DgvDelays)
        With item4
            .Cells(1).Value = (ws.Cells("b5").Value.ToString())
            .Cells(2).Value = (ws.Cells("c5").Value.ToString())
            .Cells(3).Value = (ws.Cells("D5").Value.ToString())
            .Cells(4).Value = (ws.Cells("E5").Value.ToString())
            .Cells(5).Value = (ws.Cells("F5").Value.ToString())
        End With
        '---add the row---
        FrmDas.DgvDelays.Rows.Add(item4)

        item5.CreateCells(FrmDas.DgvDelays)
        With item5
            .Cells(1).Value = (ws.Cells("b6").Value.ToString())
            .Cells(2).Value = (ws.Cells("c6").Value.ToString())
            .Cells(3).Value = (ws.Cells("D6").Value.ToString())
            .Cells(4).Value = (ws.Cells("E6").Value.ToString())
            .Cells(5).Value = (ws.Cells("F6").Value.ToString())
        End With
        '---add the row---
        FrmDas.DgvDelays.Rows.Add(item5)

    End Sub


Any help would be life saving :)

Thank you.
Posted

For i=1 to 5
        Dim item As New DataGridViewRow
        item.CreateCells(FrmDas.DgvDelays)
        With item
            .Cells(1).Value = (ws.Cells("b"+(i+1)).Value.ToString())
            .Cells(2).Value = (ws.Cells("c"+(i+1)).Value.ToString())
            .Cells(3).Value = (ws.Cells("D"+(i+1)).Value.ToString())
            .Cells(4).Value = (ws.Cells("E"+(i+1)).Value.ToString())
            .Cells(5).Value = (ws.Cells("F"+(i+1)).Value.ToString())
        End With
        '---add the row---
        FrmDas.DgvDelays.Rows.Add(item)
End For


Check the syntax for the concatenation of b2,c2 etc
 
Share this answer
 
v3
Comments
brzt 8-Jan-13 3:34am    
Thank you! I Have Made a Few Small Changes To Make It Work But You Where Pretty Much Bang On The Money
VB
For I = 1 To 5
    Dim item As New DataGridViewRow
    item.CreateCells(FrmDas.DgvDelays)
    With item
        .Cells(1).Value = (ws.Cells("b" & (I + 1)).Value.ToString())
        .Cells(2).Value = (ws.Cells("c" & (I + 1)).Value.ToString())
        .Cells(3).Value = (ws.Cells("D" & (I + 1)).Value.ToString())
        .Cells(4).Value = (ws.Cells("E" & (I + 1)).Value.ToString())
        .Cells(5).Value = (ws.Cells("F" & (I + 1)).Value.ToString())
    End With
    '---add the row---
    FrmDas.DgvDelays.Rows.Add(item)
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900