vb.net DatagridViewComboboxCell selectedindexchanged 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18983485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:12:59  来源:igfitidea点击:

DatagridViewComboboxCell selectedindexchanged does not work

vb.netdatagridviewvisual-studio-2005datagridviewcomboboxcell

提问by fcartu

I've been working in a project with VS2005 and VB.NET, in this project I have a DataGridView with 3 DataGridViewComboboxCell inside of it. What I have been trying to do is make it so that when the user selects a value from the first DataGridViewComboboxCell the value of the 2 other cells change. This is the code for that part:

我一直在使用 VS2005 和 VB.NET 参与一个项目,在这个项目中我有一个 DataGridView,里面有 3 个 DataGridViewComboboxCell。我一直在尝试做的是,当用户从第一个 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

The first problem that I have is that when I open the list of the combobox and type Ito select one item of the combobox that start with I. The combobox doesn't refresh to select this item and always goes back to the first item in the combobox, so what I do to make this work was change the value of the combobox on his own SelectedIndexChange like this:

我遇到的第一个问题是,当我打开组合框列表并键入I以选择以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

The second problem is when I type another letter to select an item that starts with that letter and without pressing enter or clicking in the item that I changed to another DatagridViewComboboxCell the combobox that raises the SelectedIndexChanged event again which changes its value to the first item in the combobox.

第二个问题是,当我键入另一个字母来选择以该字母开头的项目时,没有按 Enter 键或单击更改为另一个 DatagridViewComboboxCell 的项目,组合框再次引发 SelectedIndexChanged 事件,该事件将其值更改为第一个项目组合框。

Any idea why this happens?

知道为什么会这样吗?

回答by WozzeC

You are quite close. This is what I did to the selectedIndexChanged to make it update.

你很接近。这就是我对 selectedIndexChanged 所做的以使其更新。

I've noticed that the problem seems to lie in the line combo.SelectedValue. This value never change unless you leave the cell. What you need to use is combo.SelectedItem which changes everytime you change the combobox.

我注意到问题似乎出在combo.SelectedValue 行上。除非您离开单元格,否则此值永远不会更改。您需要使用的是 combo.SelectedItem 每次更改组合框时都会更改。

Try this instead:

试试这个:

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

I changed the way you are checking if it is a Long. If it passes the Long.TryParse then it is a long. This code works for me to change the value of other Cells if this doesn't fix it, then I'm not sure what will. I also altered all SelectedValue to SelectedItem since SelectedValue always holds the last selected Value, and not the current.

我改变了你检查它是否是 Long 的方式。如果它通过 Long.TryParse 那么它是一个长的。如果这不能解决此问题,则此代码可用于更改其他单元格的值,然后我不确定会发生什么。我还将所有 SelectedValue 更改为 SelectedItem,因为 SelectedValue 始终保存最后选择的值,而不是当前值。