×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help loading into datagridviews

Help loading into datagridviews

Help loading into datagridviews

(OP)
I have created multiple datagridview at design time named dgv1 thru dgv50.
And the following records from a datatable dt

Column1 Column2

123 0
138 1
367 2
278 3
367 4
259 5
.... ..
145 9

Need to do a for loop to populate the dgv's with column1 data such that each dgv has three columns as

dgv1 has 1 2 3
dgv2 1 3 8

I started with this code, which I got from my previous post where I tried to populate multiple textboxes.

For i = 0 To dt.Rows.Count - 1
Dim name As String = "dgv" & (i + 1).ToString
Dim dgv = Me.Controls.Find(name, True)
If dgv.Length > 0 Then

..........................

end if

Hoping not to do an if..else or Select..Case, if I can help it.

Also, I would like to add one (is this even possible) oncellclick handler for all of the dgv's

Any help will be greatly appreciated.




RE: Help loading into datagridviews

Do you want 3 columns is each dgv, and each columns is *named* from the data, or do you want 3 columns each *containing* the data? If you want the second, what should the columns be named?

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!

RE: Help loading into datagridviews

(OP)
I need to have the 3 datagrid columns to contain the data in columns1 of the datatable (the string integer will be split). I will not have a datagridview column header or name.

RE: Help loading into datagridviews


You can create a datatable, add columns to it, add data to the table and then bind it to the datagrid:

CODE

Dim s As String
Dim dc As DataColumn
Dim dtData As Datatable
Dim dr As DataRow
Dim dgv As DataGridView
Dim name As String

For i = 0 To dt.Rows.Count - 1
name = "dgv" & (i + 1).ToString
dgv = Nothing
dgv = Me.Controls.Find(name, True)
If dgv IsNot Nothing Then
s = dt.Rows(i).Item(0).ToString
dtData = New DataTable
dc = New DataColumn("Field1")
dtData.Columns.Add(dc)
dc = New DataColumn("Field2")
dtData.Columns.Add(dc)
dc = New DataColumn("Field3")
dtData.Columns.Add(dc)
dr = dtData.NewRow
dr.item(0) = s.Substring(0, 1)
dr.item(1) = s.Substring(1, 1)
dr.item(2) = s.Substring(2, 1)
dtData.Rows.Add(dr)
dgv.DataSource = dtData
EndIf
Next

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!

RE: Help loading into datagridviews

(OP)
Hi Jebenson, thanks for the code. I don't know if I revised the code correctly, but I was already getting an error on the third line of this revised code:

Dim Name As String = "dgv" & (i + 1).ToString
Dim dgv As DataGridView = Nothing
dgv = Me.Controls.Find(Name, True)

Error is :Value of type '1-dimensional array of System.Windows.Forms.

RE: Help loading into datagridviews

(OP)
BTW, I was trying to revise it because I was getting error on the variables not being declared. Thanks.

RE: Help loading into datagridviews


Sorry, I forgot that Controls.Find returns an array of type Control. Declare a dgvs() variable as an array of type Control:

Dim dgvs() As Control

Next, create a DataGridView variable:

Dim ThisDgv As DataGridView

Then change the If back to what you had before:

If dgv.Length > 0 Then

Then, after creating the DataTable, get the dgv:

ThisDgv = CType(dgvs(0), DataGridView)

ThisDgv.DataSource = dt

That should work.

The advantage of converting the control in dgvs() to a variable of DataGridView is that you can have the control as the Type you want, with access to all its methods, etc.


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!

RE: Help loading into datagridviews

(OP)
Worked like a charm! Thank you very much.

RE: Help loading into datagridviews

(OP)
I am trying to add dgv_CellContentClick handler to each of datagridview, but was getting
error: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'
I am hoping not to create individual handler for each datagridview

For i = 0 To dt.Rows.Count - 1
Dim Name As String = "dgv" & (i + 1).ToString
Dim dgvs() As Control = Me.Controls.Find(Name, True)

If dgvs.Length > 0 Then

Dim thisDGV As DataGridView = CType(dgvs(0), DataGridView)
...CODE LO POPULATE DATAGRIDVIEW
thisDGV.ClearSelection()

AddHandler thisDGV.Click, AddressOf dgv_CellContentClick

End If
Next

Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Dim dgv As New DataGridView
dgv = CType(sender, DataGridView)
MsgBox(dgv.Name)
End Sub

RE: Help loading into datagridviews

Change the Add Handler line:

AddHandler thisDGV.Click, AddressOf dgv_CellContentClick

to this:

AddHandler thisDGV.CellContentClick, AddressOf dgv_CellContentClick

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!

RE: Help loading into datagridviews

(OP)
Thank you so much again, jebenson!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close