私は VS2005 と VB.NET を使用したプロジェクトで作業してきました。このプロジェクトでは、その中に 3 つの DataGridViewComboboxCell を持つ DataGridView があります。私がやろうとしているのは、ユーザーが最初の DataGridViewComboboxCell から値を選択すると、他の 2 つのセルの値が変化するようにすることです。これはその部分のコードです:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If (TypeOf e.Control Is DataGridViewComboBoxEditingControl) AndAlso _
DataGridView1.CurrentCell.ColumnIndex = 1 Then
Dim cbo As ComboBox = TryCast(e.Control, ComboBox)
If (cbo IsNot Nothing) Then
RemoveHandler cbo.SelectedIndexChanged, AddressOf comboBox_SelectedIndexChanged
AddHandler cbo.SelectedIndexChanged, AddressOf comboBox_SelectedIndexChanged
End If
End If
End Sub
Private Sub comboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim combo As ComboBox = TryCast(sender, ComboBox)
Dim rowIndex As Long = DataGridView1.CurrentRow.Index
Dim valueType As Type = GetType(Long)
If (combo.SelectedValue IsNot Nothing) Then
Dim comboValueType As Type = combo.SelectedValue.GetType()
Dim p As Boolean = valueType.Equals(comboValueType)
If Not valueType.Equals(comboValueType) Then
Exit Sub
End If
End If
DataGridView1.Rows(rowIndex).Cells(2).Value = 'Some DB query to retrieve the value
End Sub
私が抱えている最初の問題は、コンボボックスのリストを開いて、I
で始まるコンボボックスの1つのアイテムを選択するように入力することI
です。コンボボックスはこの項目を選択するために更新されず、常にコンボボックスの最初の項目に戻ります。そのため、この作業を行うために私がしたことは、自分の SelectedIndexChange でコンボボックスの値を次のように変更することでした。
Private Sub comboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim combo As ComboBox = TryCast(sender, ComboBox)
Dim rowIndex As Long = DataGridView1.CurrentRow.Index
Dim valueType As Type = GetType(Long)
If (combo.SelectedValue IsNot Nothing) Then
Dim comboValueType As Type = combo.SelectedValue.GetType()
Dim p As Boolean = valueType.Equals(comboValueType)
If Not valueType.Equals(comboValueType) Then
Exit Sub
End If
End If
DataGridView1.Rows(rowIndex).Cells(1).Value = combo.SelectedValue
DataGridView1.Rows(rowIndex).Cells(2).Value = 'Some DB query to retrieve the value
End Sub
2番目の問題は、その文字で始まる項目を選択するために別の文字を入力し、Enterキーを押すか、別の DatagridViewComboboxCell に変更した項目をクリックせずに SelectedIndexChanged イベントを再度発生させ、その値を最初の項目に変更するコンボボックスです。コンボボックス。
なぜこれが起こるのか分かりますか?