datagrid combobox

G

Guest

I have derived a ComboBoxColumnStyle that inherits DataGridColumnStyle. It works fine except for one behavior. If the user selects a new value from the ComboBox's pulldown list on a brand new record, the ComboBoxColumn's Commit event doesn't fire. If the user first edits a TextBox cell on the same New row, and THEN selects a new value for the ComboBox cell, Commit fires. What is going on and how do I get around it

Thanks

Pat
 
C

Cor Ligthert

Hi Pat,

I had this problems as well with I thought that was orignal the one you
mentioned, however it is already a year ago, (when you change the original
for firing alone it will shows the combobox to early in the header of the
datagrid).

I made some changes and it did work in my opinion as I wished, however I
never finished this project and therefore I am not complete sure if it
works.

Maybe you can try it?

Cor

Option Strict On
Option Explicit On
Public Class DataGridCombo
Inherits ComboBox
Public Sub New()
MyBase.New()
End Sub
End Class
Public Class DataGridComboColumnStyle
Inherits DataGridColumnStyle
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridCombo
Private mDisplayMember As String
Private mValueMember As String
Private mSource As CurrencyManager
Private mRowNum As Integer
Private OldVal As String = String.Empty
Private InEdit As Boolean = False
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember As
Integer, ByVal ValueMember As Integer)
Combo = New DataGridCombo
mDisplayMember = DataSource.Columns.Item(DisplayMember).ToString
mValueMember = DataSource.Columns.Item(ValueMember).ToString
Combo.Visible = True
Combo.DataSource = DataSource
Combo.DisplayMember = mDisplayMember
Combo.ValueMember = mValueMember
Combo.DropDownStyle = ComboBoxStyle.DropDownList
Combo.Bounds = Rectangle.Empty
End Sub
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember As
String, ByVal ValueMember As String)
Combo = New DataGridCombo
mDisplayMember = DisplayMember
mValueMember = ValueMember
Combo.Visible = True
Combo.DataSource = DataSource
Combo.DisplayMember = mDisplayMember
Combo.ValueMember = mValueMember
Combo.DropDownStyle = ComboBoxStyle.DropDownList
Combo.Bounds = Rectangle.Empty
End Sub
Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
Debug.WriteLine("Abort()")
RollBack()
HideComboBox()
EndEdit()
End Sub
Protected Overloads Overrides Function Commit(ByVal DataSource As
CurrencyManager, ByVal RowNum As Integer) As Boolean
HideComboBox()
If InEdit Then
Try
Dim Value As Object = Combo.SelectedValue
If NullText.Equals(Value) Then
Value = DBNull.Value
End If
SetColumnValueAtRow(DataSource, RowNum, Value)
Catch
RollBack()
Return False
End Try
EndEdit()
End If
Return True
End Function
Protected Overloads Overrides Sub ConcedeFocus()
Combo.Visible = False
InEdit = False
End Sub
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)
mSource = Source
mRowNum = Rownum
Combo.Text = ""
Dim OriginalBounds As Rectangle = Bounds
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
Dim TextObject As Object = GetColumnValueAtRow(Source, Rownum)
If Not TextObject Is System.DBNull.Value Then Combo.SelectedValue =
TextObject.ToString()
If Not InstantText Is Nothing Then
Combo.SelectedValue = InstantText
End If
Combo.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
If InstantText Is Nothing Then
Combo.SelectAll()
Else
Combo.Select(Combo.Text.Length, 0)
End If
If Combo.Visible Then
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeight() As Integer
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
Do Until NewLineIndex = -1
NewLineIndex = DirectCast(Value, String).IndexOf("r\n",
NewLineIndex + 1)
NewLines += 1
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))
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)
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)
Dim objText As Object = GetColumnValueAtRow(Source, RowNum)
Dim Text As String = LookupDisplayValue(objText)
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)
Dim objText As Object = GetColumnValueAtRow(Source, RowNum)
Dim Text As String = LookupDisplayValue(objText)
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Dg As
DataGrid)
MyBase.SetDataGridInColumn(Dg)
If Not Combo.Parent Is Dg Then
If Not Combo.Parent Is Nothing Then
Combo.Parent.Controls.Remove(Combo)
End If
End If
If Not Dg Is Nothing Then Dg.Controls.Add(Combo)
End Sub
Protected Overloads Overrides Sub UpdateUI(ByVal Source As
CurrencyManager, ByVal RowNum As Integer, ByVal InstantText As String)
Combo.Text = GetText(GetColumnValueAtRow(Source, RowNum))
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
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 Sub EndEdit()
InEdit = False
Invalidate()
End Sub
Private Function GetText(ByVal Value As Object) As String
If Value Is System.DBNull.Value Then Return NullText
If Not Value Is Nothing Then
Return Value.ToString
Else
Return String.Empty
End If
End Function
Private Sub HideComboBox()
If Combo.Focused Then
Me.DataGridTableStyle.DataGrid.Focus()
End If
Combo.Visible = False
End Sub
Private Sub RollBack()
Combo.Text = OldVal
End Sub
Private Sub PaintText(ByVal g As Graphics, ByVal Bounds As Rectangle,
ByVal Text As String, ByVal AlignToRight As Boolean)
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 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)
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 Sub ComboChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Combo.SelectionChangeCommitted
ColumnStartedEditing(Combo)
SetColumnValueAtRow(mSource, mRowNum, Combo.SelectedValue)
End Sub
Private Function LookupDisplayValue(ByVal LookupObject As Object) As
String
If LookupObject Is System.DBNull.Value Then Return NullText
Dim LookupText As String = LookupObject.ToString
Dim I As Integer
Dim IMax As Integer = Combo.Items.Count - 1
Dim DT As DataTable = CType(Combo.DataSource, DataTable)
For I = 0 To IMax
If DT.Rows(I)(mValueMember).ToString = LookupText Then
Return DT.Rows(I)(mDisplayMember).ToString
End If
Next
Return String.Empty
End Function
End Class
 
G

Guest

Almost. I needed to reverse the order of statements in your ComboChanged event. Otherwise, when I performed the edit, the ComboBox always contained the text of the first value in its Item list. Since my ColumnStyle differs slightly from yours in other places to (namely the Edit Event), you may possibly not observe this behavior. Anyway, for me at least, it should read

Private Sub ComboChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectionChangeCommitte
SetColumnValueAtRow(_source, _rowNum, Combo.SelectedValue
ColumnStartedEditing(Combo
End Su

You had

Private Sub ComboChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectionChangeCommitte
ColumnStartedEditing(Combo
SetColumnValueAtRow(_source, _rowNum, Combo.SelectedValue
End Su

Now it works like a champ. Thanks, Co

Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top