1

私は 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 イベントを再度発生させ、その値を最初の項目に変更するコンボボックスです。コンボボックス。

なぜこれが起こるのか分かりますか?

4

1 に答える 1

1

あなたはかなり近いです。これは、selectedIndexChanged を更新するために行ったことです。

問題は行の中にあるように見えることに気付きましたcombo.SelectedValue. この値は、セルを離れない限り変わりません。使用する必要があるのは、コンボボックスを変更するたびに変更される、combo.SelectedItem です。

代わりにこれを試してください:

Private Sub comboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim combo As ComboBox = TryCast(sender, ComboBox)
Dim test As Long
If (combo.SelectedItem IsNot Nothing) Then   
    If Not Long.TryParse(combo.SelectedItem, test) Then
            Exit Sub
    End If
End If
DataGridView2.CurrentCell.Value = combo.SelectedItem
DataGridView1.Rows(rowIndex).Cells(1).Value = combo.SelectedItem
DataGridView1.Rows(rowIndex).Cells(2).Value = 'Some DB query to retrieve the value

Long かどうかを確認する方法を変更しました。Long.TryParse を渡す場合、それは long です。このコードは、これで修正されない場合に他のセルの値を変更するために機能しますが、どうなるかわかりません。また、すべての SelectedValue を SelectedItem に変更しました。これは、SelectedValue が現在ではなく、最後に選択された値を常に保持するためです。

于 2013-09-25T10:02:49.220 に答える