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!

DataGrid combobox not showing 2

Status
Not open for further replies.

NovCoder

Programmer
Jun 3, 2005
8
0
0
US
Copy and paste this code on a form with a datagrid:"dg".
It has no errors so I am not sure why the columns does not display. If you can help me I would love it.

Public Class DataGridComboBoxColumn

Inherits DataGridTextBoxColumn

Public MyCombo As DataGridComboBox
Private m_isEditing As Boolean

Public Sub New()
MyBase.New()

MyCombo = New DataGridComboBox
m_isEditing = False

AddHandler MyCombo.Leave, New _
EventHandler(AddressOf LeaveComboBox)
AddHandler MyCombo.SelectionChangeCommitted, New _
EventHandler(AddressOf OnSelectionChangeCommitted)
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As _
CurrencyManager, ByVal rowNum As Integer, ByVal _
bounds As Rectangle, ByVal readOnly1 As Boolean, _
ByVal instantText As String, ByVal cellIsVisible As _
Boolean)
MyBase.Edit(source, rowNum, bounds, _
readOnly1, instantText, cellIsVisible)

MyCombo.Parent = Me.TextBox.Parent
MyCombo.Location = Me.TextBox.Location
MyCombo.Size = New Size(Me.TextBox.Size.Width, _
MyCombo.Size.Height)
MyCombo.Text = Me.TextBox.Text
Me.TextBox.Visible = False
MyCombo.Visible = True
MyCombo.BringToFront()
MyCombo.Focus()

End Sub

Private Sub LeaveComboBox(ByVal sender As _
Object, ByVal e As EventArgs)
MyCombo.Hide()
End Sub

Protected Overloads Overrides Function Commit(ByVal _
dataSource As CurrencyManager, ByVal rowNum As _
Integer) As Boolean

If m_isEditing Then
m_isEditing = False
SetColumnValueAtRow(dataSource, rowNum, _
MyCombo.Text)
End If
Return True

End Function
Private Sub OnSelectionChangeCommitted(ByVal sender As _
Object, ByVal e As EventArgs)

m_isEditing = True
MyBase.ColumnStartedEditing(sender)

End Sub
End Class


Public Class DataGridComboBox
Inherits ComboBox

Private WM_KEYUP As Integer = &H101

Protected Overrides Sub WndProc(ByRef m _
As System.Windows.Forms.Message)
If m.Msg = WM_KEYUP Then
Return
End If
MyBase.WndProc(m)
End Sub

End Class


Public Class dgComboBoxColumn
Inherits DataGridTextBoxColumn

Public MyCombo As ComboBox

Public Sub New()
MyBase.New()

MyCombo = New ComboBox
AddHandler MyCombo.Leave, New EventHandler( _
AddressOf LeaveComboBox)

End Sub

Protected Overloads Overrides Sub Edit(ByVal _
source As CurrencyManager, ByVal rowNum _
As Integer, ByVal bounds As Rectangle, _
ByVal readOnly1 As Boolean, ByVal _
instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, _
readOnly1, instantText, cellIsVisible)

MyCombo.Parent = Me.TextBox.Parent
MyCombo.Location = Me.TextBox.Location
MyCombo.Size = New Size(Me.TextBox.Size.Width, _
MyCombo.Size.Height)
MyCombo.Text = Me.TextBox.Text
Me.TextBox.Visible = False
MyCombo.Visible = True
MyCombo.BringToFront()
MyCombo.Focus()

End Sub

Private Sub LeaveComboBox(ByVal sender _
As Object, ByVal e As EventArgs)
MyCombo.Hide()
End Sub
End Class


Sub mytest()
Dim styles As New DataGridTableStyle
styles.MappingName = "Authors"

Dim colAuthor As New DataGridTextBoxColumn
colAuthor.MappingName = "Author"
colAuthor.HeaderText = "Author"
colAuthor.Width = 200
styles.GridColumnStyles.Add(colAuthor)

' Dim dg As New DataGrid
dg.TableStyles.Add(styles)

Dim colCombo As DataGridComboBoxColumn
colCombo = New DataGridComboBoxColumn
colCombo.MappingName = "Role"
colCombo.HeaderText = "Role"
colCombo.Width = 150

colCombo.MyCombo.Items.Clear()
colCombo.MyCombo.Items.Add("Test1")
colCombo.MyCombo.Items.Add("Test2")
colCombo.MyCombo.Items.Add("Test3")
colCombo.MyCombo.Items.Add("Test4")
colCombo.MyCombo.Items.Add("Test5")


colCombo.MyCombo.DropDownStyle = _
ComboBoxStyle.DropDownList

styles.GridColumnStyles.Add(colCombo)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mytest()
End Sub


...and I'm out. O-BOY.
 
Hello,

This is some interesting code and something I will be looking at more closely as I haven't had much opportunity to do stuff like this. The only thing I noticed was this line:
Code:
MyCombo.Size = New Size(Me.TextBox.Size.Width, _
               MyCombo.Size.Height)
Is it possible you meant
Code:
MyCombo.Size = New Size(Me.TextBox.Size.Width, _
               Me.TextBox.Size.Height)
and the unitialized Size is zero? Good Luck!


Have a great day!

j2consulting@yahoo.com
 
Sorry, when I copied your code and used it, it worked. This line
Code:
dg.TableStyles.Add(styles)
needs to be just before your End Sub. As currently written, it is added before your colCombo is created. Nice work, deserving of a star even if it is Friday and you missed a couple small items ;-).

BTW, if NovCoder is short for NoviceCoder, you need a new handle!

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top