473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

datagrid combobox

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
Nov 20 '05 #1
2 2099
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(OriginalBou nds)
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

Nov 20 '05 #2
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
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gamze | last post by:
Hi, How can i get values from datagrid to combobox and should select the same name as in datagrid row on the combobox control In my vb.net windows application ,i have combobox which is...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
2
by: Bill C. | last post by:
Hi, I'm trying to implement a ComboBox drop-down column for a DataGrid. When a cell is selected in the ComboBox column I overlay a ComboBox over the cell and call: this.comboBox.Show();...
3
by: Kevin | last post by:
Hi Al I want to add two combobox columns in my datagrid. the one combobox column must be bound to the same datasource that the datagrid is, and the other combobox I just want to populate with a...
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
3
by: Doug | last post by:
Hi I have the following code (not mine) that populates a datagrid with some file names. But I want to replace the datagrid with a combo box. private void OnCurrentDataCellChanged(object sender,...
1
by: jimb | last post by:
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. -- .....
4
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.