Cor: DataGridComboColumnStyle

  • Thread starter Thread starter Aaron Smith
  • Start date Start date
A

Aaron Smith

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
 
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.
 
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.....
 
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
 
I have the same problem and posted about it twice and got no answer. I was
just going to intercept the "tab" key and move it over manually, but I can
not even figure out how to catch the "tab" key for the column because the
keypress doesn't fire when you hit tab. If anyone can capture the "tab" key
when presssed on a datagrid, please enlighten me.

I banged my head against it for days and then just told my boss it was a
feature.
Chris
 
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
 
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).ToString
_ValueMember = DataSource.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 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(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 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 said:
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,

I had not a situation with two comboboxes side by side with this, so I had
to make it, now I have it.

(For the next time)

Hower nice you solved it.

(No problem really smilling)

:-)

Cor
 
Cor, two things.

1. can you explain what is going on in the background to make this happen?
2. The solution that Aaron came up with is:
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

Isn't this the same event that you would catch if you overrode the keyup
event? Or am I mixing two concepts I have no business mixing.

Chris
 
Chris,

In my opinion are we talking about different things.

You are not the only one who is looking to get the control over the tabbing
in a datagrid.

I did not see as well a solution for that.

There are some links telling about that change, however than it is as far as
I have seen about the position change or rowindex change or about a value
change in a column (item)

Even when you catch the tab key, you have to know where it is and the
columns are resizable, looks for me a hell of a job.

Sorry, I cannot help you (yet) with this.

Cor
 
I tied adding your code to my inherited combobox but it did not help. My
implimentation is a little different than yours as I overrode the
datagridcolumnstyle class, not the datagridtextboxcolumn class to put my
combo box into the grid. I'm going to have to take some time to look in
depth at Cor's solution to see if I can swap it out for mine. It's going to
have to wait a few days as I don't have the time do devote to it now.

Thanks for all you insights though. At least I know there is a solution
now.
Chris
 
Chris,

Thinkink it over I thought I had once made a sample that used the
datacolumns events.
However I am not very happy with it, maybe I look today to it, trying to get
it better, however maybe you can use it.

\\\
Private WithEvents dtbCol1 As DataGridTextBox
Private ToolTip1 As New ToolTip
')
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Datagrid1.ReadOnly = True
Dim ts As New DataGridTableStyle
ts.MappingName = ds.Tables(0).TableName
Dim column As New DataGridTextBoxColumn
ts.GridColumnStyles.Add(column)
DataGrid1.TableStyles.Add(ts)
column = DirectCast(ts.GridColumnStyles(0), DataGridTextBoxColumn)
dtbCol1 = DirectCast(column.TextBox, DataGridTextBox)
column.MappingName = ds.Tables(0).Columns(0).ColumnName
column.HeaderText = "Cor"
column.Width = 30
dv = New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
End Sub
Private Sub dtbCol1_ToolTip(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles dtbCol1.MouseEnter
ToolTip1.SetToolTip(DirectCast(sender, DataGridTextBox), _
"Row: " & DataGrid1.CurrentRowIndex + 1)
End Sub
///

I hope this helps?

Cor
 

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

Back
Top