Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bind string array to DataGridView

Status
Not open for further replies.

kadara

Programmer
Mar 2, 2012
26
0
0
DE
Hi,

I have a function that creates a string array with 6 elements.
I would like to create a DataGridView with 10 columns and bind strings arrays to each column (the results of the function).
How could I do that?

Thanks.
 
You can't do what you want directly. I suggest creating a DataTable, adding your data to it and using that for the DataGridView's DataSource.

Something like this:

Code:
Dim dt As DataTable
Dim Column1 As DataColumn
Dim Column2 As DataColumn
Dim Column3 As DataColumn
Dim dr As DataRow

Column1 = New DataColumn("Column1")
Column2 = New DataColumn("Column2 ")
Column3 = New DataColumn("Column3")

dt = New DataTable

dt.Columns.Add(Column1)
dt.Columns.Add(Column2)
dt.Columns.Add(Column3)


For i As Integer = 0 to 5

    dr = dt.NewRow
    
    dr.Item("Column1") = Array1(i)
    dr.Item("Column2") = Array2(i)
    dr.Item("Column3") = Array3(i)

    dt.Rows.Add(dr)

Next i

DataGridView1.DataSource = dt

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top