Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day, all. I am a new VB.NET beginner. I have an issue to have a loop for iteration for my datagridview data table, which is the data from the excel file.

For example: The table looks like this:

Col1 |Col2 |Col3 | Col4
a;b  |c;d  |A001 |C004
a;b  |c;d  |B001 |C005


I would like the output to become like this:

Col1 |Col2 |Col3 |Col4
a    |c    |A001 |C004
b    |d    |A001 |C004
a    |d    |A001 |C004
b    |c    |A001 |C004
a    |c    |B001 |C005
b    |d    |B001 |C005
a    |d    |B001 |C005
b    |c    |B001 |C005

Which means each row of the data of Col1 and Col2 will be separated and iterate to become 4 rows in datagridview.

Currently, I can display the data as datatable to datagridview only, without the loop of iteration on the datagridview row. May I know how should the coding look like for the loop to iterate on the data to 4 rows? I am very weak at VB.NET coding, so I need help with the coding.

Can someone provide me some coding examples so that I can have an idea of how to write the loop to iterate on the data as soon as possible? I had stuck in this issue for a few days already.

And I appreciate all the help!!

What I have tried:

VB
For k = 0 To DataGridView1.Rows.Count - 1
    Dim j As Integer = 1

    For j = 1 To 4

        DataGridView1.Rows.Add(row)
        j += 1
    Next
Next
Posted
Updated 24-Nov-20 3:00am
v3
Comments
CHill60 24-Nov-20 7:19am    
What is the "certain reason" that you cannot show us your code. It makes it very difficult for us to help you without it
Member 14969271 24-Nov-20 8:36am    
Sorry that because the code is not at my side just now. I had updated the question on what I had tried.
Richard MacCutchan 24-Nov-20 7:57am    
There is no reason at all why you cannot show the relevant parts of your code. And without that information it is impossible to make useful suggestions.
Member 14969271 24-Nov-20 8:36am    
Sorry that because the code is not at my side just now. I had updated the question on what I had tried.
Richard MacCutchan 24-Nov-20 9:08am    
You need to extract columns 1 and 2 and create the sets you want: so it is the first character of each set, then the second, then the first and second, and finally the second and first. So creating a simple loop to get those parts should not be too difficult.

1 solution

Take your input, and split each column one in two, duplicating the test of the row into a new output collection. This will give you twice the rows.
Then loop through that collection, doing the same exact thing but with column two. This will give you a collection with 4 times the elements you started with.

Display the final collection.

When you've got that working, it should be reasonably obvious how you could improve that to do it with one loop: split column one, generate two rows. Split each of those to give two more rows. But get the "simple" version working first!
 
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