Public Class Form2
Private dtRegion As New DataTable("dtRegion") 'ภาค
Private dtZone As New DataTable("dtZone") 'เขตการขาย/โซน
Private parentBS As New BindingSource() 'อนุมานว่า ภาค
Private childBS As New BindingSource() 'อนุนมานว่า เขตการขาย/โซน
Private filteredChildBS As New BindingSource() 'อนุมานได้ว่า ภาค/โซน
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'add columns/rows in the Region table
dtRegion.Columns.Add(New DataColumn() With {.ColumnName = "RegionID", .DataType = GetType(Integer)})
dtRegion.Columns.Add(New DataColumn() With {.ColumnName = "RegionDescription", .DataType = GetType(String)})
dtRegion.Rows.Add(1, "ภาคเหนือ")
dtRegion.Rows.Add(2, "ภาคใต้")
dtRegion.Rows.Add(3, "ภาคตะวันตก")
dtRegion.Rows.Add(4, "ภาคตะวันออก")
'add some column/rows in the Zone table
dtZone.Columns.Add(New DataColumn() With {.ColumnName = "ZoneID", .DataType = GetType(Integer)})
dtZone.Columns.Add(New DataColumn() With {.ColumnName = "ZoneDescription", .DataType = GetType(String)})
dtZone.Columns.Add(New DataColumn() With {.ColumnName = "RegionID", .DataType = GetType(Integer)})
dtZone.Rows.Add(1, "ภาคเหนือ ผิวขาว", 1)
dtZone.Rows.Add(2, "ภาคเหนือ อวบอูม", 1)
dtZone.Rows.Add(1, "ภาคใต้ ผิวขาว", 2)
dtZone.Rows.Add(2, "ภาคใต้ หอยงาม", 2)
dtZone.Rows.Add(1, "ภาคตะวันตก ผิวขาว", 3)
dtZone.Rows.Add(2, "ภาคตะวันตก หอยใหญ่", 3)
dtZone.Rows.Add(1, "ภาคตะวันออก ผิวขาว", 4)
dtZone.Rows.Add(2, "ภาคตะวันออก ระยองฮิสั้น จันฮิยาว", 4)
dtZone.Rows.Add(3, "ภาคตะวันออก หอยหอม", 4)
'Bound to a datatable
Dim dtYET As New DataTable("dtYET")
dtYET.Columns.Add(New DataColumn() With {.ColumnName = "RegionID", .DataType = GetType(Integer)})
dtYET.Columns.Add(New DataColumn() With {.ColumnName = "ZoneID", .DataType = GetType(Integer)})
'**************************************
'Tricks
'**************************************
dtYET.Rows.Add(1, 1)
dtYET.Rows.Add(2, 2)
dtYET.Rows.Add(3, 3)
dtYET.Rows.Add(4, 4)
' set up ParentBS and ChildBS and filteredChildBS
parentBS.DataSource = dtRegion
childBS.DataSource = dtZone
filteredChildBS.DataSource = dtZone
'bind the regionComboBoxColumn to the parentBS
Me.regionComboBoxColumn.DataSource = parentBS
Me.regionComboBoxColumn.DisplayMember = "RegionDescription"
Me.regionComboBoxColumn.ValueMember = "RegionID"
'bind the ZoneComboBoxColumn to the childBS
Me.ZoneComboBoxColumn.DataSource = childBS
Me.ZoneComboBoxColumn.DisplayMember = "ZoneDescription"
Me.ZoneComboBoxColumn.ValueMember = "ZoneID"
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = dtYET
DataGridView1.Columns(0).DataPropertyName = "RegionID"
DataGridView1.Columns(1).DataPropertyName = "ZoneID"
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Try
If e.ColumnIndex = regionComboBoxColumn.Index Then
If Me.DataGridView1.DataSource IsNot Nothing Then
If Me.DataGridView1.DataSource.GetType() = GetType(DataTable) Then
Me.DataGridView1.Rows(e.RowIndex).Cells(ZoneComboBoxColumn.Index).Value = DBNull.Value
Else
Me.DataGridView1.Rows(e.RowIndex).Cells(ZoneComboBoxColumn.Index).Value = Nothing
End If
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
If e.ColumnIndex <> ZoneComboBoxColumn.Index Then
'MessageBox.Show("หอยงาม")
End If
End Sub
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If Me.DataGridView1.CurrentCell.ColumnIndex = ZoneComboBoxColumn.Index Then
Dim control As DataGridViewComboBoxEditingControl = TryCast(e.Control, DataGridViewComboBoxEditingControl)
Dim bs As BindingSource = TryCast(control.DataSource, BindingSource)
If bs IsNot Nothing Then
' set the filteredChildBS as the DataSource of the editing control
TryCast(e.Control, ComboBox).DataSource = filteredChildBS
Dim regionvalue As Object = Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).Cells(regionComboBoxColumn.Index).Value
If regionvalue Is DBNull.Value OrElse regionvalue Is Nothing Then
filteredChildBS.Filter = "RegionID=-1"
Else
filteredChildBS.Filter = "RegionID=" + regionvalue.ToString()
End If
' set the SelectedValue property of the editing control
If Me.DataGridView1.CurrentCell.Value IsNot DBNull.Value AndAlso Me.DataGridView1.CurrentCell.Value IsNot Nothing Then
control.SelectedValue = Me.DataGridView1.CurrentCell.Value
End If
End If
End If
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Enter Then
SendKeys.Send("{TAB}")
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
<ToolboxBitmap(GetType(DataGridView))> _
Public Class usrCtrGridControlExt : Inherits DataGridView
'...
'...
'...
'
End Class