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
valueMem = _valueMember.Replace(_dataMember, "").Remove(0, 1)
Else
valueMem = _valueMember
End If
If _displayMember.IndexOf(_dataMember) > -1 Then
'remove the Parent.Relation. from the display member
displayMem = _displayMember.Replace(_dataMember, "").Remove(0, 1)
Else
displayMem = _displayMember
End If
'make sure it's not a added row
If Not value Is Nothing AndAlso Not value Is System.DBNull.Value Then
If drv(valueMem) = value Then
Try
s = drv(displayMem)
Catch ex As Exception
s = value
End Try
Exit For
End If
Else
s = Me.NullText
Exit For
End If
Next
g.FillRectangle(backBrush, rect)
rect.Offset(0, 2)
rect.Height -= 2
If Not s Is Nothing Then
If value Is Nothing Then
s = NullText
End If
Try
g.DrawString(s.ToString, Me.DataGridTableStyle.DataGrid.Font, foreBrush,
RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End If
End Sub
Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
MyBase.SetDataGridInColumn(value)
If Not (_comboBox.Parent Is Nothing) Then
_comboBox.Parent.Controls.Remove(_comboBox)
End If
If Not (value Is Nothing) Then
value.Controls.Add(_comboBox)
End If
End Sub
Public Overrides Property Width() As Integer
Get
Return MyBase.Width
End Get
Set(ByVal Value As Integer)
MyBase.Width = Value
_comboBox.Width = Value - 4
End Set
End Property
Private Function GetText(ByVal value As Object) As String
If value Is System.DBNull.Value Then
Return Me.NullText
Else
If Not value Is Nothing Then
Return value.ToString
Else
Return String.Empty
End If
End If
End Function
Private Sub _comboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles _comboBox.SelectedIndexChanged
_isEditing = True
MyBase.ColumnStartedEditing(_comboBox)
End Sub
End Class
Best regards,
Rick