I have derived a ComboBoxColumnS tyle that inherits DataGridColumnS tyle. 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 2 2124
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 DataGridComboCo lumnStyle
Inherits DataGridColumnS tyle
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.Colu mns.Item(Displa yMember).ToStri ng
mValueMember = DataSource.Colu mns.Item(ValueM ember).ToString
Combo.Visible = True
Combo.DataSourc e = DataSource
Combo.DisplayMe mber = mDisplayMember
Combo.ValueMemb er = mValueMember
Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
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.DataSourc e = DataSource
Combo.DisplayMe mber = mDisplayMember
Combo.ValueMemb er = mValueMember
Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
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.SelectedV alue
If NullText.Equals (Value) Then
Value = DBNull.Value
End If
SetColumnValueA tRow(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(x Margin, 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 = GetColumnValueA tRow(Source, Rownum)
If Not TextObject Is System.DBNull.V alue Then Combo.SelectedV alue =
TextObject.ToSt ring()
If Not InstantText Is Nothing Then
Combo.SelectedV alue = InstantText
End If
Combo.RightToLe ft = Me.DataGridTabl eStyle.DataGrid .RightToLeft
If InstantText Is Nothing Then
Combo.SelectAll ()
Else
Combo.Select(Co mbo.Text.Length , 0)
End If
If Combo.Visible Then
DataGridTableSt yle.DataGrid.In validate(Origin alBounds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeigh t() As Integer
Return Combo.Preferred Height + yMargin
End Function
Protected Overloads Overrides Function GetPreferredHei ght(ByVal g As
Graphics, ByVal Value As Object) As Integer
Debug.WriteLine ("GetPreferredH eight()")
Dim NewLineIndex As Integer = 0
Dim NewLines As Integer = 0
Do Until NewLineIndex = -1
NewLineIndex = DirectCast(Valu e, String).IndexOf ("r\n",
NewLineIndex + 1)
NewLines += 1
Loop
Return FontHeight * NewLines + yMargin
End Function
Protected Overloads Overrides Function GetPreferredSiz e(ByVal g As
Graphics, ByVal Value As Object) As Size
Dim Extents As Size = Size.Ceiling(g. MeasureString(G etText(Value), _
Me.DataGridTabl eStyle.DataGrid .Font))
Extents.Width += xMargin * 2 + DataGridTableGr idLineWidth
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 = GetColumnValueA tRow(Source, RowNum)
Dim Text As String = LookupDisplayVa lue(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 = GetColumnValueA tRow(Source, RowNum)
Dim Text As String = LookupDisplayVa lue(objText)
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInCo lumn(ByVal Dg As
DataGrid)
MyBase.SetDataG ridInColumn(Dg)
If Not Combo.Parent Is Dg Then
If Not Combo.Parent Is Nothing Then
Combo.Parent.Co ntrols.Remove(C ombo)
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(GetColu mnValueAtRow(So urce, RowNum))
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
Private ReadOnly Property DataGridTableGr idLineWidth() As Integer
Get
If Me.DataGridTabl eStyle.GridLine Style = DataGridLineSty le.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.V alue 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.DataGridTabl eStyle.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.D ataGridTableSty le.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(Me.D ataGridTableSty le.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_Implic it(Rect) ' Convert to
RectangleF
Dim Format As StringFormat = New StringFormat
If AlignToRight Then
Format.FormatFl ags = StringFormatFla gs.DirectionRig htToLeft
End If
Select Case Me.Alignment
Case Is = HorizontalAlign ment.Left
Format.Alignmen t = StringAlignment .Near
Case Is = HorizontalAlign ment.Right
Format.Alignmen t = StringAlignment .Far
Case Is = HorizontalAlign ment.Center
Format.Alignmen t = StringAlignment .Center
End Select
Format.FormatFl ags = Format.FormatFl ags Or StringFormatFla gs.NoWrap
g.FillRectangle (Brush:=BackBru sh, Rect:=Rect)
Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Te xt, Me.DataGridTabl eStyle.DataGrid .Font, ForeBrush,
RectF, Format)
Format.Dispose( )
End Sub
Private Sub ComboChanged(By Val sender As Object, ByVal e As
System.EventArg s) Handles Combo.Selection ChangeCommitted
ColumnStartedEd iting(Combo)
SetColumnValueA tRow(mSource, mRowNum, Combo.SelectedV alue)
End Sub
Private Function LookupDisplayVa lue(ByVal LookupObject As Object) As
String
If LookupObject Is System.DBNull.V alue Then Return NullText
Dim LookupText As String = LookupObject.To String
Dim I As Integer
Dim IMax As Integer = Combo.Items.Cou nt - 1
Dim DT As DataTable = CType(Combo.Dat aSource, DataTable)
For I = 0 To IMax
If DT.Rows(I)(mVal ueMember).ToStr ing = LookupText Then
Return DT.Rows(I)(mDis playMember).ToS tring
End If
Next
Return String.Empty
End Function
End Class
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(By Val sender As Object, ByVal e As System.EventArg s) Handles Combo.Selection ChangeCommitte
SetColumnValueA tRow(_source, _rowNum, Combo.SelectedV alue
ColumnStartedEd iting(Combo
End Su
You had
Private Sub ComboChanged(By Val sender As Object, ByVal e As System.EventArg s) Handles Combo.Selection ChangeCommitte
ColumnStartedEd iting(Combo
SetColumnValueA tRow(_source, _rowNum, Combo.SelectedV alue
End Su
Now it works like a champ. Thanks, Co
Pat This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 populated by
sqlserver database table.When i select value from combobox ,value saved in to
other table of my database
and i use to datagrid to show this table.
|
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
several examples on the web. They all seem to have problems, though
that I've been unable to resolve.
The most promising example I have found is at:
|
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();
this.comboBox.BringToFront();
this.comboBox.Focus();
|
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 whole lot of values, and then when the user selects a value, I want it to filter the possible values in the other combobox. Is this possible? Also this unbound combobox that hold the whole lot of values, I want to insert it next to the combobox that...
|
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 download a fully working C# sample with the Northwind.mdb here:
www.insightgis.com.au/web/stuff/DataGridCombo.zip
| |
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 how to use this code? please
help!
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20862953.html
|
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:
http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx
I have a problem when there are two DataGrid's on one form, and when I
switch focus from one grid to the other. To be more precise, when I'm editing
a combo box column in one grid, and then click in the combo column of...
|
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, System.EventArgs e)
{try{
DataSet ds = dgMembers.DataSource as DataSet;
|
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.
--
..
http://sf-f.org, weblog and search engine for fans and writers of
|
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 combobox in a datagrid?
Any other suggestions/articles on how to implement many-many relations in
the frontend (which of course are 2 one-many relations)?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |