Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
 
    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 Smile | :)
 
Thank you.
Posted 7-Jan-13 19:58pm
brzt409
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v3
Comments
brzt at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 288
1 _Amy 270
2 Maciej Los 220
3 Manfred R. Bihy 200
4 CHill60 180
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,347
2 Maciej Los 3,799
3 Peter Leow 3,558
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100