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

Adding Controls to a DataGrid at Runtime

Status
Not open for further replies.

Coppermill

Programmer
Oct 5, 2004
21
GB
Any one know how in VB.NET to add controls to a DataGrid at Runtime, which enable you to change the current cell to say a combobox?
 
All these example are creating the DataGrid, but not changing them at RunTime.

The problem comes when the DataGrid is already filled and then you need to change the DataGrid format to another type such as a Combo.

I've seen a C# example, but having troubles transfering to VB.Net

 
hey buddy,
What i did was just create a combo box and then resize and place the combo box on the position in the grid.
 
Can you show me the VB.Net code that you did to do this?
 
I'm not using a combobox, i'm using number boxes in my grid so somebody doesn't enter letters. I have done the same thing only with a combobox before. All you need to do is replace TDBQtyPacked with you combobox name. If you need anymore help please let me know. I hope this helps, its not the best way, but its better than spending the money on third party controls.

here is my code:
Private Sub dgPacking_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgPacking.MouseUp
'Get size and location of grid cell
Dim cb As New System.Drawing.Rectangle 'cell bounds
Dim cb2 As New System.Drawing.Rectangle



If dgPacking.CurrentCell.ColumnNumber = 3 And dgPacking.CurrentCell.RowNumber = dgPacking.CurrentRowIndex Then

'TDBDQtyPacked.Visible = True
If Not TDBDQtyPacked.Tag = "Open" Then
cb = dgPacking.GetCellBounds(dgPacking.CurrentRowIndex, 3)
TDBDQtyPacked.Location = New Point(cb.X, cb.Y)
TDBDQtyPacked.Size = New Size(cb.Width, cb.Height)

TDBDQtyPacked.Value = dgPacking.Item(dgPacking.CurrentRowIndex, 3)

dgPacking.Controls.Add(TDBDQtyPacked)
'TDBDQtyPacked.BringToFront()
'Must Add the events you want to use

AddHandler TDBDQtyPacked.Change, AddressOf TDBDQtyPacked_Change
Else
' 'Same as above "With out Addhandler if control is already open"
cb = dgPacking.GetCellBounds(dgPacking.CurrentRowIndex, 3)
TDBDQtyPacked.Location = New Point(cb.X, cb.Y)
TDBDQtyPacked.Size = New Size(cb.Width, cb.Height)

TDBDQtyPacked.Value = dgPacking.Item(dgPacking.CurrentRowIndex, 3)

dgPacking.Controls.Add(dgNumbox)
End If
TDBDQtyPacked.Visible = True

End If

If dgPacking.CurrentCell.ColumnNumber = 2 And dgPacking.CurrentCell.RowNumber = dgPacking.CurrentRowIndex Then
If Not TDBTimeEach.Tag = "Open" Then
cb2 = dgPacking.GetCellBounds(dgPacking.CurrentRowIndex, 2)
TDBTimeEach.Location = New Point(cb2.X, cb2.Y)
TDBTimeEach.Size = New Size(cb2.Width, cb2.Height)
TDBTimeEach.Value = dgPacking.Item(dgPacking.CurrentRowIndex, 2)
dgPacking.Controls.Add(TDBTimeEach)

AddHandler TDBTimeEach.Change, AddressOf TDBTimeEach_Change

Else
cb2 = dgPacking.GetCellBounds(dgPacking.CurrentRowIndex, 2)
TDBTimeEach.Location = New Point(cb2.X, cb2.Y)
TDBTimeEach.Size = New Size(cb2.Width, cb2.Height)
TDBTimeEach.Value = dgPacking.Item(dgPacking.CurrentRowIndex, 2)
dgPacking.Controls.Add(TDBTimeEach)
End If
TDBTimeEach.Visible = True

End If

End Sub

Sincerely,
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top