Groups | Blog | Home
all groups > vb.net > march 2006 >

vb.net : Custom DataGRidComboBoxStyle DataSource Problems


Ken Tucker [MVP]
3/1/2006 5:48:02 PM
Hi,

http://www.vb-tips.com/default.aspx?ID=e51432cb-e517-4bae-bc26-7320e41e6d05

Ken
------------------
[quoted text, click to view]

Richard Ryerson
3/1/2006 8:15:43 PM
I have a general DataGridComboBoxColumn that I built using the Example in
the .NET 2003 Combined Collection help file (that was a data time picker).

I am able to assign a data source and display / value members so the
ComboBox is data bound and that works, to a point. What generally works is
the ComboBox get's populated and the ComboBox shows itself when the
appropriate column/row is selected. When the ComboBoxColumn isn't selected
it displays the correct text value (not the related key id value). Alls
cool, until I bind with a DataSet that contains multiple tables related with
DataRelations.

If there are multiple rows, The last row selected properly displays it's
combobox values / or data values. The other rows display blanks. (I set the
comboboxcolumn to display blanks when it can't find the correct value in the
dataset/table/relation.)


I believe this is because of the DataRelation. There is relational data
between on column and another, I.e. a customer ship to location applies to a
single customer. When the last selected row is customer Ford, then the
Chrysler records display blanks in the customer ship to .

Because of the DataRelation it is limiting the data that each combo box is
privy to. And every combo box (shipto's) shares the data source, I believe.

Anyone have any good ideas on how to 'fix' this anomoly? Any help is greatly
appreciated.

Best Regards,

Rick

Richard Ryerson
3/2/2006 12:00:00 AM
Here is the scenario.

Customers (Id, Name)
-------------
1, Ford
2, Chrysler

ShipTos (Id, CustId, Location)
---------
1, 1, XYZ
2, 1, PDQ
3, 2, FAQ
4, 2, ROF

There is a relation between Customers and ShipTos (Customers.Id ->
ShipTos.CustId)

Now main DataTable (Releases) contains info pertaining to Customers and
ShipTos

Releases (Id, CustId, ShipToId, Qty)
----------
1, 1, 1, 5000 (1, Ford, XYZ, 5000)
2, 2, 4, 2500 (2, Chrysler, ROF, 2500)

When the data grid first displays, it only displays complete information for
the second row, because this is the last row in the grid, and therefore the
last DataRelation view set i'll call it. The First record does not display
correctly because it's data is violating the DataRelation constraints.
(ShipTo XYZ does not relate to Chrysler). So, that column gets displayed as
a blank, because to display the 'word' you must traverse or find the 'word'
by the key (1) in the current view.

It appears that each ComboBox 'shares' it's properties / data whatever
regardless of the row. I need a way to search the correct Relation or
remember (bad code) the previous data relation information. I've tried
storing the data/state in a hashtable, but it seems to not work.

Here is the code for the ComboBoxColumn. It needs to be as extendable /
useful as possible. Meaning I can't design this comboboxstyle to work ONLY
in this situation.

Best regards,

Rick

Imports System.ComponentModel

Public Class DataGridComboBoxColumn

Inherits DataGridColumnStyle

Private _isEditing As Boolean = False

Private WithEvents _comboBox As ComboBox

Private _dataSource As Object

Private _dataMember As String

Private _valueMember As String

Private _displayMember As String

Private _lists As Hashtable

Protected Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)

Select Case e.PropertyName

Case "DataSource"

_comboBox.DataSource = _dataSource

Case "ValueMember"

If _dataMember Is Nothing Then

_comboBox.ValueMember = _valueMember

Else

_comboBox.ValueMember = String.Format("{0}.{1}", _dataMember, _valueMember)

End If

Case "DisplayMember"

If _dataMember Is Nothing Then

_comboBox.DisplayMember = _displayMember

Else

_comboBox.DisplayMember = String.Format("{0}.{1}", _dataMember,
_displayMember)

End If

End Select

Invalidate()

End Sub

Public ReadOnly Property DataBindings() As ControlBindingsCollection

Get

Return _comboBox.DataBindings

End Get

End Property

Public Property DataSource() As Object

Get

Return _dataSource

End Get

Set(ByVal Value As Object)

_dataSource = Value

OnPropertyChanged(New PropertyChangedEventArgs("DataSource"))

End Set

End Property

Public Property ValueMember() As String

Get

Return _valueMember

End Get

Set(ByVal Value As String)

_valueMember = Value

OnPropertyChanged(New PropertyChangedEventArgs("ValueMember"))

End Set

End Property

Public Property DisplayMember() As String

Get

Return _displayMember

End Get

Set(ByVal Value As String)

_displayMember = Value

OnPropertyChanged(New PropertyChangedEventArgs("DisplayMember"))

End Set

End Property

Public Property DataMember() As String

Get

Return _dataMember

End Get

Set(ByVal Value As String)

_dataMember = Value

OnPropertyChanged(New PropertyChangedEventArgs("DataMember"))

End Set

End Property

Public Sub New()

_comboBox = New ComboBox

_lists = New Hashtable

_comboBox.Visible = False

End Sub

Protected Overrides Sub Abort(ByVal rowNum As Integer)

_isEditing = False

DataGridTableStyle.DataGrid.Invalidate(_comboBox.Bounds)

End Sub

Protected Overrides Function Commit(ByVal dataSource As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean

_comboBox.Bounds = Rectangle.Empty

If Not _isEditing Then

Return True

End If

_isEditing = False

Try

Dim value As Object

value = _comboBox.SelectedValue

If NullText.Equals(value) Then

value = System.Convert.DBNull

End If

SetColumnValueAtRow(dataSource, rowNum, value)

Catch ex As Exception

End Try

DataGridTableStyle.DataGrid.Invalidate(_comboBox.Bounds)

Return True

End Function

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

Dim value As Object = GetText(GetColumnValueAtRow(source, rowNum))

If cellIsVisible Then

_comboBox.Bounds = New Rectangle(bounds.X + 2, bounds.Y + 2, bounds.Width -
4, bounds.Height - 4)

_comboBox.Visible = True

Else

_comboBox.Visible = False

End If

If _comboBox.Visible Then

DataGridTableStyle.DataGrid.Invalidate(bounds)

End If

End Sub

Protected Overrides Function GetMinimumHeight() As Integer

Return _comboBox.PreferredHeight + 4

End Function

Protected Overrides Function GetPreferredHeight(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As Integer

Return _comboBox.PreferredHeight + 4

End Function

Protected Overrides Function GetPreferredSize(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

Return New Size(_comboBox.Width + 4, _comboBox.PreferredHeight + 4)

End Function

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)

Paint(g, bounds, [source], rowNum, False)

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
alignToRight As Boolean)

Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, alignToRight)

End Sub

Protected Overloads Overrides 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 value As Object = GetColumnValueAtRow([source], rowNum)

Dim rect As Rectangle = bounds

Dim s As String = Nothing

Dim displayMem As String

Dim valueMem As String

If _lists(rowNum) Is Nothing Then

Dim col(_comboBox.Items.Count - 1) As Object

_comboBox.Items.CopyTo(col, 0)

_lists.Add(rowNum, col)

End If

'loop through the items of the combo box

'this items collection seems to be shared

'between all the instances of the same

'column of combo boxes.

Dim col2() As Object = _lists(rowNum)

For Each drv As DataRowView In _comboBox.Items

If _valueMember.IndexOf(_dataMember) > -1 Then

'remove the Parent.Relation. from the value member

Richard Ryerson
3/2/2006 3:44:29 AM
Yeah, I am able to display the "words" but state is being lost when selected
"row" changes in the datagrid, due to the relation performing it's job. I
need a viable way to save previous states and call upon that.


[quoted text, click to view]

AddThis Social Bookmark Button