473,484 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2104
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
1969
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
4233
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
2477
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
2398
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
3012
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
4315
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
6803
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
2984
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
2348
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
2256
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
7079
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6949
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6809
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4838
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3044
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3038
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1355
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.