I got the original from here:
http://tinyurl.com/6dvvd
There are two different column styles on that page. So here is what I
finally came up with:
This does not have the tab problem when it's a drop down list.
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
'ignore keyup to avoid problem with tabbing & dropdownlist;
Return
End If
MyBase.WndProc(m)
End Sub 'WndProc
Public Sub New()
MyBase.New()
End Sub
Private _modified As Boolean = False
Public isInEditOrNavigateMode As Boolean = True
Public Property Modified() As Boolean
Get
Return _modified
End Get
Set(ByVal Value As Boolean)
_modified = Value
End Set
End Property
End Class
<DataSysDescription("This column style adds a ComboBox to the Column")> _
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
'
' UI constants
'
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridComboBox
Private WithEvents tb As TextBox = Me.TextBox
Private _DisplayMember As String
Private _ValueMember As String
Private _rowNum As Integer
Private WithEvents _source As CurrencyManager
'
' Used to track editing state
'
Private OldVal As String = String.Empty
Private InEdit As Boolean = False
'
' Create a new column - DisplayMember, ValueMember
' Passed by ordinal
'
Public Sub New()
Combo = New DataGridComboBox
Combo.Visible = False
End Sub
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember
As Integer, ByVal ValueMember As Integer)
Debug.WriteLine("New(1)")
Combo = New DataGridComboBox
_DisplayMember =
DataSource.Columns.Item(index:=DisplayMember).ToSt ring
_ValueMember = DataSource.Columns.Item(index:=ValueMember).ToStri ng
With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = _DisplayMember
.ValueMember = _ValueMember
End With
End Sub
'
' Create a new column - DisplayMember, ValueMember
' passed by string
'
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember
As String, ByVal ValueMember As String)
Debug.WriteLine("New(2)")
Combo = New DataGridComboBox
With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = DisplayMember
.ValueMember = ValueMember
End With
End Sub
Public Sub New(ByRef DataSource As DataView, ByVal DisplayMember As
Integer, ByVal ValueMember As Integer)
Debug.WriteLine("New(3)")
Combo = New DataGridComboBox
_DisplayMember =
DataSource.Table.Columns.Item(index:=DisplayMember ).ToString()
_ValueMember =
DataSource.Table.Columns.Item(index:=ValueMember). ToString()
With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = _DisplayMember
.ValueMember = _ValueMember
End With
End Sub
'
' Create a new column - DisplayMember, ValueMember
' passed by string
'
Public Sub New(ByRef DataSource As DataView, ByVal DisplayMember As
String, ByVal ValueMember As String)
Debug.WriteLine("New(4)")
Combo = New DataGridComboBox
With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = DisplayMember
.ValueMember = ValueMember
End With
End Sub
'------------------------------------------------------
' Methods overridden from DataGridColumnStyle
'------------------------------------------------------
'
' Abort Changes
'
Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
Debug.WriteLine("Abort()")
RollBack()
HideComboBox()
EndEdit()
End Sub
'
' Commit Changes
'
Protected Overloads Overrides Function Commit(ByVal DataSource As
CurrencyManager, ByVal RowNum As Integer) As Boolean
Debug.WriteLine("Commit()")
HideComboBox()
_rowNum = RowNum
_source = DataSource
If Not InEdit Then
Return True
End If
Try
Dim Value As Object = Combo.SelectedValue
If NullText.Equals(Value) Then
Value = Convert.DBNull
End If
tb.Text = Combo.Text
SetColumnValueAtRow(DataSource, RowNum, Value)
Catch e As Exception
RollBack()
Return False
End Try
EndEdit()
Return True
End Function
'
' Remove focus
'
Protected Overloads Overrides Sub ConcedeFocus()
Debug.WriteLine("ConcedeFocus()")
Combo.Visible = False
End Sub
'
' Edit Grid
'
Protected Overloads Overrides Sub Edit(ByVal Source As
CurrencyManager, ByVal Rownum As Integer, ByVal Bounds As Rectangle,
ByVal [ReadOnly] As Boolean, ByVal InstantText As String, ByVal
CellIsVisible As Boolean)
Debug.WriteLine("Edit()")
Combo.Text = String.Empty
Dim OriginalBounds As Rectangle = Bounds
Dim txt As String
OldVal = Combo.Text
If CellIsVisible Then
Bounds.Offset(xMargin, yMargin)
Bounds.Width -= xMargin * 2
Bounds.Height -= yMargin
Combo.Bounds = Bounds
Combo.Visible = True
Else
Combo.Bounds = OriginalBounds
Combo.Visible = False
End If
txt = tb.Text
If Not txt = InstantText Then
Combo.Modified = True
End If
If Not txt = NullText Then
Combo.SelectedValue = GetText(GetColumnValueAtRow(Source,
Rownum))
End If
If Not InstantText Is Nothing Then
Combo.SelectedValue = InstantText
End If
Combo.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
Combo.Focus()
If InstantText Is Nothing Then
Combo.SelectAll()
Else
Dim [End] As Integer = Combo.Text.Length
Combo.Select([End], 0)
End If
If Combo.Visible Then
DataGridTableStyle.DataGrid.Invalidate(OriginalBou nds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeight() As Integer
'
' Set the minimum height to the height of the combobox
'
Debug.WriteLine("GetMinimumHeight()")
Return Combo.PreferredHeight + yMargin
End Function
Protected Overloads Overrides Function GetPreferredHeight(ByVal g
As Graphics, ByVal Value As Object) As Integer
Debug.WriteLine("GetPreferredHeight()")
Dim NewLineIndex As Integer = 0
Dim NewLines As Integer = 0
Dim ValueString As String = Me.GetText(Value)
Do
While NewLineIndex <> -1
NewLineIndex = ValueString.IndexOf("r\n", NewLineIndex + 1)
NewLines += 1
End While
Loop
Return FontHeight * NewLines + yMargin
End Function
Protected Overloads Overrides Function GetPreferredSize(ByVal g As
Graphics, ByVal Value As Object) As Size
Dim Extents As Size =
Size.Ceiling(g.MeasureString(GetText(Value),
Me.DataGridTableStyle.DataGrid.Font))
Debug.WriteLine("GetPreferredSize()")
Extents.Width += xMargin * 2 + DataGridTableGridLineWidth
Extents.Height += yMargin
Return Extents
End Function
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal
Bounds As Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As
Integer)
Debug.WriteLine("Paint(1)")
Paint(g, Bounds, Source, RowNum, False)
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal
Bounds As Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As
Integer, ByVal AlignToRight As Boolean)
Debug.WriteLine("Paint(2)" &
GetText(GetColumnValueAtRow(Source, RowNum)))
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum))
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Sub Paint(ByVal g As Graphics, ByVal Bounds As
Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As Integer,
ByVal BackBrush As Brush, ByVal ForeBrush As Brush, ByVal AlignToRight
As Boolean)
Debug.WriteLine("Paint(3)")
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum))
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Value
As DataGrid)
Debug.WriteLine("SetDataGridInColumn()")
MyBase.SetDataGridInColumn(Value)
If Not (Combo.Parent Is Value) Then
If Not (Combo.Parent Is Nothing) Then
Combo.Parent.Controls.Remove(Combo)
End If
End If
If Not (Value Is Nothing) Then Value.Controls.Add(Combo)
If Not (tb.Parent Is Value) Then
If Not (tb.Parent Is Nothing) Then
tb.Parent.Controls.Remove(tb)
End If
End If
If Not (Value Is Nothing) Then Value.Controls.Add(tb)
End Sub
Protected Overloads Overrides Sub UpdateUI(ByVal Source As
CurrencyManager, ByVal RowNum As Integer, ByVal InstantText As String)
Debug.WriteLine("UpdateUI()")
Combo.Text = tb.Text
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
'----------------------------------------------------------------------
' Helper Methods
'----------------------------------------------------------------------
Public Property ComboBox() As DataGridComboBox
Get
Return Combo
End Get
Set(ByVal Value As DataGridComboBox)
Combo = Value
End Set
End Property
Private ReadOnly Property DataGridTableGridLineWidth() As Integer
Get
If Me.DataGridTableStyle.GridLineStyle =
DataGridLineStyle.Solid Then
Return 1
Else
Return 0
End If
End Get
End Property
Private Shadows Sub EndEdit()
Debug.WriteLine("EndEdit()")
InEdit = False
Combo.Modified = False
Invalidate()
End Sub
Private Function GetText(ByVal Value As Object) As String
If Not Value Is Nothing Then
Debug.WriteLine("GetText(" & Value.ToString & ")")
Else
Debug.WriteLine("GetText(Value is Nothing)")
End If
If Value Is System.DBNull.Value Then Return NullText
If Not Value Is Nothing Then
Debug.WriteLine(Value.ToString)
Return Value.ToString
Else
Debug.WriteLine("Value is Nothing")
Return String.Empty
End If
End Function
Private Sub HideComboBox()
Debug.WriteLine("HideComboBox()")
If Combo.Focused Then
Me.DataGridTableStyle.DataGrid.Focus()
End If
Combo.Visible = False
End Sub
Private Sub RollBack()
Debug.WriteLine("RollBack()")
Combo.Text = OldVal
tb.Text = OldVal
Combo.Modified = False
End Sub
Private Shadows Sub PaintText(ByVal g As Graphics, ByVal Bounds As
Rectangle, ByVal Text As String, ByVal AlignToRight As Boolean)
Debug.WriteLine("PaintText(1)")
Dim BackBrush As Brush = New
SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(Me.DataGridTableStyle.ForeColor)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub
Private Shadows Sub PaintText(ByVal g As Graphics, ByVal TextBounds
As Rectangle, ByVal Text As String, ByVal BackBrush As Brush, ByVal
ForeBrush As Brush, ByVal AlignToRight As Boolean)
Debug.WriteLine("PaintText(2)" & Text)
Dim Rect As Rectangle = TextBounds
Dim RectF As RectangleF = RectF.op_Implicit(Rect) ' Convert to
RectangleF()
Dim Format As StringFormat = New StringFormat
If AlignToRight Then
Format.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
Select Case Me.Alignment
Case Is = HorizontalAlignment.Left
Format.Alignment = StringAlignment.Near
Case Is = HorizontalAlignment.Right
Format.Alignment = StringAlignment.Far
Case Is = HorizontalAlignment.Center
Format.Alignment = StringAlignment.Center
End Select
Format.FormatFlags = Format.FormatFlags Or StringFormatFlags.NoWrap
g.FillRectangle(Brush:=BackBrush, Rect:=Rect)
Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Text, Me.DataGridTableStyle.DataGrid.Font,
ForeBrush, RectF, Format)
Format.Dispose()
End Sub
Private Function GetColumnTextAtRow(ByVal Source As
CurrencyManager, ByVal RowNum As Integer) As Object
Dim value As Object = Me.GetColumnValueAtRow(Source, RowNum)
Dim dSource As Object
dSource = Combo.DataSource
If value Is System.DBNull.Value Then
Return NullText
End If
If Not dSource Is Nothing Then
If TypeOf dSource Is DataTable Then
Dim dr As DataRow
dSource = CType(dSource, DataTable)
If Not dSource.Columns.Contains(Combo.DisplayMember) _
OrElse Not dSource.Columns.Contains(Combo.ValueMember) Then
Return NullText
End If
For Each dr In dSource.Rows
If value = dr(Combo.ValueMember) Then
Return dr(Combo.DisplayMember)
End If
Next
Return NullText
ElseIf TypeOf dSource Is DataView Then
Dim drv As DataRowView
dSource = CType(dSource, DataView)
If Not
dSource.Table.Columns.Contains(Combo.DisplayMember ) _
OrElse Not
dSource.Table.Columns.Contains(Combo.ValueMember) Then
Return NullText
End If
For Each drv In dSource
If value = drv(Combo.ValueMember) Then
Return drv(Combo.DisplayMember)
End If
Next
Return NullText
End If
End If
End Function
Private Sub Combo_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Combo.SelectedIndexChanged
tb.Text = CType(sender, ComboBox).Text
End Sub
End Class
Chris, Master of All Things Insignificant wrote:
Do you have a link to the orginal post Cor did? I have an overriden
combobox and a datagridcolumnstyle, but you solution to the tab problem did
not help mine. The keyup event never fired when I use tab. I'm missing
something simple here I'm sure.
Chris
"Aaron Smith" <th**********@smithcentral.net> wrote in message
news:PQ*******************@newssvr33.news.prodigy. com...
Ok. Just forget this whole thing.
Private WM_KEYUP As Integer = &H101
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
If m.Msg = WM_KEYUP Then
'ignore keyup to avoid problem with tabbing & dropdownlist;
Return
End If
MyBase.WndProc(m)
End Sub 'WndProc
In the derived combo fixes it. Thanks for letting me talk it through on
the newsgroup. lol
Aaron Smith wrote:
And if it's any clue to anyone why this is happening, if I have a break
point in the edit sub, it won't leave the combo box, it will stay there.
Take the break point out, and it will leave every time.....
Aaron Smith wrote:
Forget the first problem. Solved that by changing the inheritance from a
columnstyle to the textboxcolumn. But I still can't keep it from
focusing out of the column when I tab into it.
Aaron Smith wrote:
>Cor,
>
>I found a previous message from you on the newsgroups with code for a
>DataGridComboColumnStyle.. This one seems to work a lot better than the
>one that I wrote, however I'm having a couple of problems. One, if I
>tab into the column, it won't stay there. It immediately goes into the
>next column. The second one, is if you are not in the column, the value
>of the combo is not displaying in the column when the combo disappears.
>
>I am using relatively the same code, however I made one change. I made
>the Combobox public, so that I could modify some of it's properties
>from outside of the ColumnStyle. I did this because I wanted to set the
>selectedvalue bindings, and I couldn't find a New() method that did
>that so I cheated. :)
>
>Any help with these would be greatly appreciated.
>Thanks,
>Aaron
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.