1

コンボボックス付きのデータグリッドがあります。ここで、onが列0の行0の値xを選択した場合、列1の行0のコンボボックスの値yがxと同じ値に自動的に設定されることを実現したいと思います。

私は試した

 Private Sub dataGridView1_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles Sched_Grid.CellValidating
    If Sched_Grid.CurrentCell.ColumnIndex = 0 Then
        Sched_Grid(1, Sched_Grid.CurrentCell.RowIndex).Value = Sched_Grid(0, Sched_Grid.CurrentCell.RowIndex).Value
    End If
End Sub

ただし、すぐには機能しません。最初に値x(0,0)を選択し、次に別のセル(5,5など)をクリックしてから、x(0,0)セルに戻ってクリックし、y(1,0)がに設定されるようにする必要があります。 x(0,0)。

これはどのように解決できますか?

4

2 に答える 2

2

EditingControlShowingイベントを使用して基になるコントロールに到達してから、別のハンドラーを追加する必要があります。でも少し面倒になります。

Private Sub Sched_Grid_EditingControlShowing(ByVal sender as Object, Byval e as DataGridViewEditingControlShowingEventArgs) Handles Sched_Grid.EditingControlShowing
  If Sched_Grid.CurrentCell.ColumnIndex = 0 Then
    Dim selectedComboBox As Combobox = DirectCast(e.Control, ComboBox)
    RemoveHandler selectedCombobox.SelectionChangeCommitted, AddressOf selectedComboBox_SelectionChangeCommitted
    AddHandler selectedCombobox.SelectionChangeCommitted, AddressOf selectedComboBox_SelectionChangeCommitted
  End If
End Sub

Private Sub selectedComboBox_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)
  Dim selectedCombobox As ComboBox = DirectCast(sender, ComboBox)
  If selectedCombobox.SelectedItem IsNot Nothing Then
    Sched_Grid(1, Sched_Grid.CurrentCell.RowIndex).Value = selectedCombobox.SelectedItem
  End If
End Sub
于 2012-07-24T16:42:28.733 に答える
0

CurrencyManagerクラスを使用して、両方のコントロールを同じコレクションにバインドできる場合があります。では、 DisplayMemberプロパティをComboBox使用して、必要な列のみを表示できます。

簡単な例: http://www.vb-tips.com/CurrencyManager.aspx

于 2012-07-24T18:04:35.027 に答える