Issues on Load String Array randomly by using loop and add into datagridview

I am a VB.NET beginner. I had faced the issues on loading string array randomly and add into datagridview.

For the project, first of all, I will import the data from excel to datagridview. After that, I need to load the string array randomly and put it into the new column (ColumnC) added into datagridview. I have 4 string values, and I want the loop to be able to load 1 to 4 string values(minimum 1 to maximum 4) randomly into the column of datagridview.

For example:

Array = {"abc", "cde", "efg","hij"}

For the output, I want a random number of these 4 string values able to load into the columnC of datagridview, which mean that the output should be:


ColumnA || ColumnB || ColumnC ||

1 ||3 ||abc, cde ||

2 ||5 ||abc ||

3 ||7 ||abc, cde, efg,hij||

4 ||9 ||cde, efg ||

My code so far:

Dim rnd As New Random()
    Dim ARR As String() = {"abc", "cde", "efg", "hij"}
    For Each value In ARR.OrderBy(Function(x) Rnd.Next())
        DataGridView1.Rows(1).Cells(1).Value = value
    Next

After loading the string loop randomly, I would like to add the random value into each row of ColumnC too. From the code, I am only able to load one random string value into 1 row of ColumnC only.

Can anyone provide some coding examples or solutions so that I can solve my issue? I had stuck with these issues for a few days, and hope that I can solve these issues as soon as possible.

Thank you very much.



Read more here: https://stackoverflow.com/questions/64929978/issues-on-load-string-array-randomly-by-using-loop-and-add-into-datagridview

Content Attribution

This content was originally published by alice teh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: