# Visual Basic > Database Development >  [RESOLVED] How can I create a DataGridView row with an array?

## The_Hobbyist

Is this the correct forum for this?  Is this considered a database related question?  My appologies if I'm in the wrong place.

As suggested, I have starting making smaller, sample applications to help me understand how specific functions of VB work instead of trying to learn by using my larger, more complex application as the learning tool.  The hope is that I can get a grasp of a single function or aspect of VB by using a simple, small project to learn with then once I have some better understanding of how that particular function or aspect works, I can then incorporate it into my larger application.  That's the main goal...  With that out of the way:

I am attempting to populate a DataGridView using only an array.  The goal is to have a user enter data into 5 simple textboxes.  The text from these textboxes is then to be used to build an array.  From there I want to create a DGV row using that array.  With the code below I am able to do all of this.  My only issue is that the DGV doesn't add just one line but instead it duplicates the line over and over again.  It is my belief that I need to use some form of counter in the loop that coincides with the number of columns in the DGV?  If this is true, I'm not sure how to go about defining that counter.  If this is untrue, what do I need to do to only enter a single line into the DGV instead of duplicating it many times?



```
Public Class frmCreateArray

    Private Sub btnCreateArray_Click(sender As Object, e As EventArgs) Handles btnCreateArray.Click

        Dim FirstName As String = Me.txtFirstName.Text
        Dim MiddleName As String = Me.txtMiddleName.Text
        Dim LastName As String = Me.txtLastName.Text
        Dim PhoneNumber As String = Me.txtPhoneNumber.Text
        Dim Email As String = Me.txtEmail.Text

        Dim Person As String = (FirstName & " " & MiddleName & " " & LastName & " " & PhoneNumber & " " & Email)

        frmDGV.DGV.ColumnCount = 5
        frmDGV.DGV.Columns(0).HeaderText = "First Name"
        frmDGV.DGV.Columns(1).HeaderText = "Middle Name"
        frmDGV.DGV.Columns(2).HeaderText = "Last Name"
        frmDGV.DGV.Columns(3).HeaderText = "Phone Number"
        frmDGV.DGV.Columns(4).HeaderText = "Email"


        For Each WhyIsThisVariableNameIrrelevant In Person  '<---- I obviously require a counter of some sort here that coincides with my number of columns?

            Dim rowStringCollection As String() = Person.Split(New String() {" "c}, StringSplitOptions.RemoveEmptyEntries)
            frmDGV.DGV.Rows.Add(New String() {rowStringCollection(0), rowStringCollection(1), rowStringCollection(2), rowStringCollection(3), rowStringCollection(4)})

        Next

    End Sub

End Class
```

----------


## wes4dbt

Man you are zig zagging all over the place.  

Instead of telling you everything that's wrong with that code I'll just give you an example.  



```

Public Class Form3

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load

        Me.DataGridView1.Columns.Add("col1", "Colhdr1")
        Me.DataGridView1.Columns.Add("Col2", "colhdr2")

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fieldArray = {Me.TextBox1.Text, Me.TextBox2.Text}
        Me.DataGridView1.Rows.Add(fieldArray)
    End Sub
End Class
```

this has nothing to do with a database so these types of questions should be asked in the .Net programming forum.

----------


## The_Hobbyist

> Man you are zig zagging all over the place.  
> 
> Instead of telling you everything that's wrong with that code I'll just give you an example.  
> 
> 
> 
> ```
> 
> Public Class Form3
> ...


Thank you!  Everything about this reply was perfect!  I am zig zagging around now.  Starting little projects to hopefully understand small aspects of code with the intention to amalgamate my understanding later into a larger, more cohesive project does have me zig zagging.  The first small project was about using panels.  I managed that OK I think.  I made a simple form with panel on it.  From there I can open three different forms into that panel, one at a time.  It helped me to understand what panels do and what they may be used for.  Now I am working on how to populate datagridviews and the different ways of doing so.

I took your sample code and manipulated it to work in my scenario and it works flawlessly.  Thank you for that. My apologies for sticking this thread in the wrong forum.

----------


## wes4dbt

> From there I can open three different forms into that panel, one at a time.


You completely lost me.  Why would you put a form inside a panel.  

The whole reason we've mentioned using a panel was so you didn't have to open another form.

you put a panel on a form
You add the control you need for the search to the panel.  
Period

Edit:  Well if your just playing around I guess it's not important if I understand what your doing.  lol

----------

