帮助Nest Loop [英] Helping With Nest Loop

查看:54
本文介绍了帮助Nest Loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重写我的一些代码,使其更整洁,更高效,因为我刚刚完成了概念验证阶段,但我正在努力为下面的代码编写循环。



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 :)

Thank you.

推荐答案

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





Che ck b2,c2等的串联语法



Check the syntax for the concatenation of b2,c2 etc


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


这篇关于帮助Nest Loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆