Working Code of Bound dropdownlist in Datagrid

  • Thread starter Thread starter jimb
  • Start date Start date
J

jimb

I can get the dropdownlist into the datagrid, and I can populate it, but I
can't read it.

Anybody have a working example of a dropdownlist in an editable grid?

Thanks.
 
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q480q

http://www.syncfusion.com/faq/winforms/Files/DataGridBoundCombo_vb.zip

http://dotnet.leadit.be/extendeddatagrid

http://www.knowdotnet.com/articles/kdngrid.html



code by "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
SetColumnValueAtRow(_source, _rowNum, Combo.SelectedValue)
ColumnStartedEditing(Combo)
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


Code by "pmcguire":

Option Strict Off
Option Explicit On
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data

Namespace DataGridComboBoxColumnStyle
Public Class DataGridComboBox
Inherits ComboBox
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


Public Class DataGridComboBoxColumn
Inherits DataGridColumnStyle
'
' UI constants
'
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridComboBox
Private WithEvents tb As 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
tb = New TextBox
tb.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).ToString
_ValueMember = DataSource.Columns.Item(index:=ValueMember).ToString

With Combo
..Visible = False
..DataSource = DataSource
..DisplayMember = _DisplayMember
..ValueMember = _ValueMember
End With
tb = New TextBox
With tb
..Visible = False
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
tb = New TextBox
With tb
..Visible = False
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
tb = New TextBox
With tb
..Visible = False
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
tb = New TextBox
With tb
..Visible = False
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(OriginalBounds)
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 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 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 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

End Namespace
 
Back
Top