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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help loading into datagridviews 1

Status
Not open for further replies.

malaygal

IS-IT--Management
Feb 22, 2006
192
US
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.




 
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!
 
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.
 

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
[indent]name = "dgv" & (i + 1).ToString[/indent]
[indent]dgv = Nothing[/indent]
[indent]dgv = Me.Controls.Find(name, True)[/indent]
[indent]If dgv IsNot Nothing Then[/indent]
[indent][indent]s = dt.Rows(i).Item(0).ToString[/indent][/indent]
[indent][indent]dtData = New DataTable[/indent][/indent]
[indent][indent]dc = New DataColumn("Field1")[/indent][/indent]
[indent][indent]dtData.Columns.Add(dc)[/indent][/indent]
[indent][indent]dc = New DataColumn("Field2")[/indent][/indent]
[indent][indent]dtData.Columns.Add(dc)[/indent][/indent]
[indent][indent]dc = New DataColumn("Field3")[/indent][/indent]
[indent][indent]dtData.Columns.Add(dc)[/indent][/indent]
[indent][indent]dr = dtData.NewRow[/indent][/indent]
[indent][indent]dr.item(0) = s.Substring(0, 1)[/indent][/indent]
[indent][indent]dr.item(1) = s.Substring(1, 1)[/indent][/indent]
[indent][indent]dr.item(2) = s.Substring(2, 1)[/indent][/indent]
[indent][indent]dtData.Rows.Add(dr)[/indent][/indent]
[indent][indent]dgv.DataSource = dtData[/indent][/indent]
[indent]EndIf[/indent]
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!
 
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.
 
BTW, I was trying to revise it because I was getting error on the variables not being declared. Thanks.
 

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!
 
Worked like a charm! Thank you very much.
 
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
 
Change the Add Handler line:

AddHandler thisDGV.Click, AddressOf dgv_CellContentClick

to this:

AddHandler thisDGV.[red]CellContentClick[/red], 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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top