Hi netracer,
I achieved this with the DacDataGridComboBox class (below).
This code sets a filter for the data in the (target) combo based on the
value selected in the source combo but you should be able to adapt to meet
your needs.
in this case:
Set the ComboFilterColumn property to the name of the column the combo is
dependant on
Set the ComboFilterField property to name of the field (DataColumn) that you
want to filter.
Doug
Public Class DacDataGridComboBox
Inherits DataGridTextBoxColumn
Public ColumnComboBox As NoKeyUpCombo
Public Shared _RowCount As Integer
Private _source As System.Windows.Forms.CurrencyManager
Private _RowNum As Integer
Private _isEditing As Boolean
Private CurrentValue As Object
Private stReadOnlyColName As String
Private stReadOnlyColOperator As String
Private stReadOnlyColParameter As String
Private stDependantField As String
Private stSortField As String
Private stComboFilterField As String
Private stComboFilterColumn As String
Private stComboSortField As String
'Property for testing another column to set ReadOnly status
Public Property ReadOnlyColName() As Object
'Property to Determine which Row to test
Get
Return stReadOnlyColName
End Get
Set(ByVal Value As Object)
stReadOnlyColName = Value & ""
End Set
End Property
'Property for testing another column to set ReadOnly status
Public Property ReadOnlyColOperator() As String
'Property to Determine which Row to test
Get
Return stReadOnlyColOperator
End Get
Set(ByVal Value As String)
stReadOnlyColOperator = Value
End Set
End Property
'Property for testing another column to set ReadOnly status
Public Property ReadOnlyColParameter() As String
'Test value
Get
Return stReadOnlyColParameter
End Get
Set(ByVal Value As String)
stReadOnlyColParameter = Value
End Set
End Property
'Property for Resetting a another column if combo changes value
Public Property DependantField() As String
'Combo Feild to Filter
Get
Return stDependantField
End Get
Set(ByVal Value As String)
stDependantField = Value
End Set
End Property
'Property for filtering combo on another field
Public Property ComboFilterField() As String
'Field to Reset
Get
Return stComboFilterField
End Get
Set(ByVal Value As String)
stComboFilterField = Value
End Set
End Property
'Property for Resetting a another column if combo changes value
Public Property ComboFilterColumn() As String
'Test value
Get
Return stComboFilterColumn
End Get
Set(ByVal Value As String)
stComboFilterColumn = Value
End Set
End Property
'Property for Resetting a another column if combo changes value
Public Property ComboSortField() As String
'Test value
Get
Return stComboSortField
End Get
Set(ByVal Value As String)
stComboSortField = Value
End Set
End Property
Public Sub New()
_source = Nothing
_isEditing = False
_RowCount = -1
ColumnComboBox = New NoKeyUpCombo
ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
AddHandler ColumnComboBox.Leave, AddressOf LeaveComboBox
AddHandler ColumnComboBox.SelectionChangeCommitted, _
AddressOf ComboStartEditing
End Sub
'Hide ComboBox if scrolling
Private Sub HandleScroll(ByVal sender As Object, ByVal e As EventArgs)
If ColumnComboBox.Visible Then
ColumnComboBox.Hide()
End If
End Sub
'Set Flag IsEditing
Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)
_isEditing = True
MyBase.ColumnStartedEditing(sender)
End Sub
'Reset Flag IsEditing
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
If _isEditing Then
SetColumnValueAtRow(_source, _RowNum, ColumnComboBox.Text)
_isEditing = False
Invalidate()
End If
ColumnComboBox.Hide()
AddHandler Me.DataGridTableStyle.DataGrid.Scroll, _
New EventHandler(AddressOf HandleScroll)
End Sub
'Over ride Paint Method
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)
If Not Me.ReadOnly Then
Dim colBackCol As Color = Me.DataGridTableStyle.BackColor
Dim colAltBackCol As Color = Me.DataGridTableStyle.AlternatingBackColor
Dim bshBackCellColour, bshAltBackCellColour As SolidBrush
bshBackCellColour = New SolidBrush(colBackCol)
bshAltBackCellColour = New SolidBrush(colAltBackCol)
If rowNum / 2 > Int(rowNum / 2) Then
backBrush = bshBackCellColour
Else
backBrush = bshAltBackCellColour
End If
End If
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Sub
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)
MyBase.Edit([source], rowNum, bounds, [readOnly], _
instantText, cellIsVisible)
_RowNum = rowNum
_source = [source]
If Not Me.ReadOnly Then
With ColumnComboBox
If Not (stComboFilterField) Is Nothing And _
Not (stComboFilterColumn) Is Nothing Then
Dim stFilter As String
Dim drv As DataRowView = CType([source].Current, DataRowView)
Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)
stFilter = ComboFilterField & "='" & drv(ComboFilterColumn) & "'"
dv.RowFilter = stFilter
If Not (stComboSortField) Is Nothing Then
dv.Sort = stComboSortField
End If
..DataSource = dv
End If
..Parent = Me.TextBox.Parent
..Location = Me.TextBox.Location
..Size = New Size(Me.TextBox.Size.Width + 15, Me.TextBox.Size.Height)
..SelectedIndex = .FindStringExact(Me.TextBox.Text)
If Me.TextBox.Text.Length > 0 Then
..Text = Me.TextBox.Text
Else
..DroppedDown = True
End If
Me.TextBox.Visible = False
..Visible = True
AddHandler Me.DataGridTableStyle.DataGrid.Scroll, _
AddressOf HandleScroll
..BringToFront()
..Focus()
End With
End If
End Sub
Protected Overrides Function Commit _
(ByVal datasource As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Boolean
If _isEditing Then
_isEditing = False
SetColumnValueAtRow(datasource, rowNum, ColumnComboBox.Text)
End If
Return True
End Function
Protected Overrides Sub ConcedeFocus()
MyBase.ConcedeFocus()
End Sub
Protected Overrides Function GetColumnValueAtRow _
(ByVal [source] As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Object
'Get value for Whs for this line
Dim s As Object = MyBase.GetColumnValueAtRow([source], rowNum)
'Get the current row
Dim drv As DataRowView = CType([source].Current, DataRowView)
CurrentValue = s
'Test and Set Read Only Status if Column follows rule
Dim mTestCol As New DacTestDRColumn
With mTestCol
..DefReadOnlyFlag = Me.ReadOnly
..ReadOnlyColName = stReadOnlyColName
..ReadOnlyColOperator = stReadOnlyColOperator
..ReadOnlyColParameter = stReadOnlyColParameter
..ReadOnlyDrv = drv
Me.ReadOnly = .DacTest
End With
Return s
End Function
Protected Overrides Sub SetColumnValueAtRow _
(ByVal [source] As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer, ByVal value As Object)
Dim s As Object = value
Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)
Dim rowCount As Integer = dv.Count
Dim i As Integer = 0
Dim s1 As Object
While i < rowCount
s1 = dv(i)(Me.ColumnComboBox.DisplayMember)
If (Not s1 Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While
If i < rowCount Then
s = dv(i)(Me.ColumnComboBox.ValueMember)
Else
s = DBNull.Value
End If
MyBase.SetColumnValueAtRow([source], rowNum, s)
If CurrentValue Is DBNull.Value Then
CurrentValue = ""
End If
If s Is DBNull.Value Then
s = ""
End If
Dim drv As DataRowView = CType([source].Current, DataRowView)
If CurrentValue <> s And Not (stDependantField) Is Nothing Then
drv(stDependantField) = ""
End If
End Sub
End Class
#End Region
"NetRacer" <ne******@netspeedway.net> wrote in message
news:de*************@news.t-online.com...
hi,
for my DataGrids I'm using DataGridComboBoxColumns (the simple class
example found everywhere in the internet) for the user to select the values. this
works quite good.
now for one DataGrid I need the data in the ComboBox to depend on other
values in the row, so i have to reload the DataSource from the DB.
for example: user selects a product from group A, the ComboBox should
select and show all possible errors for group A.
i tried to overwrite the DataGridComboBoxColumn class and build the reload
into the GetDataValue function, but if i select an entry of the ComboBox,
it immediatly changes back to NULL.
does anybody have an idea or another solution for this?
thx for any help
netracer