Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: Re: Binding Datagrids and Comboboxes (New2VB)


Message #1 by "David S Jones" <dsjones@L...> on Sun, 2 Feb 2003 03:02:16
The following is a description of what I did.

Very truly yours,

David Jones


ANALYSIS
MS has dropped the ball on this and might have advertised more than it 
can deliver. Binding to the TEXT property of a Listbox is easier than to 
a comboBox. Notice that the syntax for binding requires the name of the 
property and the name of the object. This implies that there exists a one-
to-one relationship between the object and the property. There is a one-
to-one relationship between the textbox and its TEXT property. This is 
not the case with a comboBox. The comboBox is, of course, associated with 
an array of items. Each item having its own "TEXT" property. Behind the 
scenes there is an unsuccessful attempt to encapsulate the many "TEXT" 
properties to the one comboBox object. 

PROPOSAL
Create your own comboBox giving it a STRING property and then bind the 
dataview to that property. Each time the index of the comboBox changes 
you should update the value of your new STRING property to be the same as 
the TEXT property of the comboBox. Correspondingly, each time the 
dataview changes the value of the new STRING property, you should be 
alerted of that event and you should respond by updating the TEXT 
property of the comboBox.

IMPLEMENTATION
Inherit from the comboBox class. Add a property and an event signature. 
Trigger the event when the previous value of the STRING property differs 
from the newly set value.

	Public Class ComboBox
	    Inherits System.Windows.Forms.ComboBox
	    Private Index As Int32
	    Private Datum As String
	
	    Public Property SelectedTxt() As String
	        Get
	            Return Datum
	        End Get
	        Set(ByVal Value As String)
	            If Value <> Datum Then RaiseEvent txtChanged(Me, 
Datum, Value)
	            Datum = Value
	        End Set
	    End Property
	    Public Event txtChanged(ByVal sender As MyLib.ComboBox, ByVal 
before As String, ByVal after As String)
	End Class


Add an event handler into your code that will take care of the TEXT 
property of the comboBox.

    Private Sub cbx_txtChanged(ByVal sender As MyLib.ComboBox, ByVal 
before As String, ByVal after As String) Handles cbx.txtChanged
        Try
            sender.Text = after
            sender.FindStringExact(sender.Text)
        Catch exp As Exception
            MsgBox(exp.GetType.ToString & vbCr & exp.Message.ToString & 
vbCr & "cbx_txtChanged")
        End Try
    End Sub

Add an event handler into your code that will take care of the STRING 
property of the comboBox

    Private Sub cbx_SelectedIndexChanged(ByVal sender As Object, ByVal e 
As System.EventArgs) Handles cbx.SelectedIndexChanged
        Dim grid As DataGrid
        Dim box As MyLib.ComboBox = CType(sender, MyLib.ComboBox)
        Try
            grid = Data_grid_in_question
            box.SelectedTxt = box.Text
            grid.Focus()
            sender.focus()
        Catch exp As Exception
            MsgBox(exp.GetType.ToString & vbCr & exp.Message.ToString & 
vbCr & "cbx_SelectedIndexChanged")
        End Try
    End Sub

SUMMARY
This works fine. The comboBox is to limit user entry to a finite list of 
valid values. Hence allowing the user to update those fields from the 
datagrid would defeat that purpose. Therefore the requirement to change 
a "comboBox bound" field directly from the datagrid is not addressed by 
this solution.

I really wish there was a better solution.



  Return to Index