How can I simplify this For loop?

Hello,

How can I simplify the For loop code below

For i = 0 To myLocalDataSet.Tables("tblCustomer").Rows.Count - 1

myDataRow = myLocalDataSet.Tables("tblCustomer").NewRow()

myDataRow("ID") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("ID")

myDataRow("Cust_Number1") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Number1")

myDataRow("Cust_Name") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Name")

myDataRow("Cust_Address1") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Address1")

myDataRow("Cust_Address2") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Address2")

myDataRow("Cust_City") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_City")

myDataRow("Prov_ID") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Prov_ID")

myDataRow("Cust_PC1") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_PC1")

myDataRow("Cust_PC2") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_PC2")

myDataRow("Cust_Phone") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Phone")

myDataRow("Cust_Faxa") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Faxa")

myDataRow("Cust_Faxb") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Faxb")

myDataRow("Cust_Faxc") = myLocalDataSet.Tables("tblCustomer").Rows(i).Item("Cust_Faxc")

Next

... I have aproximately 25 more columns to include in this loop so if I can narrow it down, that would be great.

Thanks in advance.



Answer this question

How can I simplify this For loop?

  • Swapna.B.

    Hi,

    Looks like you could have one more inner loop to iterate the colunms collection. In that case your column names should match while assigning.

    Thanks,



  • Carl In Milford

    Ajeeth Kumar wrote:

    Hi,

    Looks like you could have one more inner loop to iterate the colunms collection. In that case your column names should match while assigning.

    Thanks,

    Ahhh... You all are great. Thanks Ajeeth!

    Here is the final code:

    For i = 0 To myLocalDataSet.Tables("tblCustomer").Rows.Count - 1

    myDataRow = myLocalDataSet.Tables("tblCustomer").NewRow()

    Dim intColNum As Integer

    For intColNum = 0 To 51

    myDataRow(intColNum) = myLocalDataSet.Tables("tblCustomer").Rows(i).Item(intColNum)

    Next

    frmMain.ProgressBar1.Value += 1

    myLocalDataSet.Tables("tblCustomer").Rows.Add(myDataRow)

    myLocalSqlDataAdapter.Update(myLocalDataSet, "tblCustomer")

    Next


  • How can I simplify this For loop?