Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 向DataGridviewComboBoxColumn添加值而不丢失焦点_Vb.net_Visual Studio 2010_Datagridview - Fatal编程技术网

Vb.net 向DataGridviewComboBoxColumn添加值而不丢失焦点

Vb.net 向DataGridviewComboBoxColumn添加值而不丢失焦点,vb.net,visual-studio-2010,datagridview,Vb.net,Visual Studio 2010,Datagridview,我的表单中有一个DataGridView,它有一个ComboBoxColumn,其中包含一个硬编码的植物列表。如果选择了“其他”,我还为用户提供了添加其他植物的选项 当用户选择“其他”时,他会在消息框中输入新的草名 但是,单击“确定”后,新名称不会添加到组合框中。(植物间距的值以编程方式添加) 只有单击表中的另一个单元格,才能添加新名称 如何在不失去焦点的情况下更新组合框 下面是单击组合框时我正在使用的代码 Private Sub DataGridView1_EditingControl

我的表单中有一个DataGridView,它有一个ComboBoxColumn,其中包含一个硬编码的植物列表。如果选择了“其他”,我还为用户提供了添加其他植物的选项

当用户选择“其他”时,他会在消息框中输入新的草名

但是,单击“确定”后,新名称不会添加到组合框中。(植物间距的值以编程方式添加)

只有单击表中的另一个单元格,才能添加新名称

如何在不失去焦点的情况下更新组合框

下面是单击组合框时我正在使用的代码

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    CB = TryCast(e.Control, System.Windows.Forms.ComboBox)
    If CB IsNot Nothing Then
        RemoveHandler CB.SelectedIndexChanged, AddressOf DGVComboIndexChanged
        AddHandler CB.SelectedIndexChanged, AddressOf DGVComboIndexChanged
    End If

    ' Other event handlers removed and added here

End Sub

Private Sub DGVComboIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    ' Do what you like with the current ComboBoxCell.
    'System.Windows.Forms.MessageBox.Show(String.Format( _
    '"The SelectedIndex was changed in ComboBoxCell: {0}" & _
    'Environment.NewLine & _
    '"The current item is: {1}", _
    'Me.DataGridView1.CurrentCellAddress.ToString(), _
    'CB.SelectedItem.ToString()))

    Dim TryAgain As Boolean = True
    Dim Letters As String = "abcdefghijklmnopqrstuvwxyz1234567890"
    Dim ComboColumn As System.Windows.Forms.DataGridViewComboBoxColumn

    ComboColumn = DataGridView1.Columns(0)
    Try
        If CB.SelectedItem.ToString = "Other" Then
            While TryAgain
                OtherGrass = Microsoft.VisualBasic.InputBox("Enter the alternate plant name", "Other plant", "")
                If OtherGrass = "" Then
                    'return the cell to ""
                    DataGridView1.CurrentRow.Cells(0).Value = ""
                    Exit Sub
                End If
                For i As Integer = 1 To Len(Letters)
                    If InStr(LCase(OtherGrass), Mid(Letters, i, 1)) > 0 Then
                        TryAgain = False
                        Exit For
                    End If
                Next
                For i As Integer = 0 To ComboColumn.Items.Count - 1
                    If LCase(OtherGrass) = LCase(ComboColumn.Items.Item(i).ToString) Then
                        TryAgain = True
                        System.Windows.Forms.MessageBox.Show("This plant has already been added.")
                    End If
                Next
            End While
            'ReDim Preserve Grasses(Grasses.GetUpperBound(0) + 1)
            'Grasses(Grasses.GetUpperBound(0)) = OtherGrass
            ComboColumn.Items.Add(OtherGrass)
        End If

        If DataGridView1.CurrentRow.Cells(1).Value Is Nothing Then
            DataGridView1.CurrentRow.Cells(1).Value = txtSpacMult.Text
        End If
        'If CB.SelectedItem.ToString <> "" Then
        '  For i As Integer = 1 To DataGridView1.Columns.Count - 2
        '    Dim temp As Boolean = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly
        '    Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly = False
        '    temp = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly
        '  Next
        'End If

        EnableRun()

    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try

End Sub
Private子DataGridView1_EditingControlShowing(ByVal sender作为对象,ByVal e作为System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)处理DataGridView1.EditingControlShowing
CB=TryCast(例如,控件、系统、窗口、窗体、组合框)
如果CB不是什么,那么
RemoveHandler CB.SelectedIndexChanged,DGVComboIndexChanged的地址
AddHandler CB.SelectedIndexChanged,DGVComboIndexChanged的地址
如果结束
'其他事件处理程序已删除并添加到此处
端接头
私有子DGVComboIndexChanged(ByVal发送方作为对象,ByVal e作为事件参数)
'使用当前ComboxCell执行您喜欢的操作。
'System.Windows.Forms.MessageBox.Show(String.Format(_
“'SelectedIndex已在ComboBoxCell:{0}”中更改&_
"Environment.NewLine,_
““当前项为:{1}”_
'Me.DataGridView1.CurrentCellAddress.ToString()_
'CB.SelectedItem.ToString()))
Dim TryAgain为布尔值=真
按String=“abcdefghijklmnopqrstuvxyz1234567890”对字母进行调校
将ComboBox列作为System.Windows.Forms.DataGridViewComboBoxColumn
ComboColumn=DataGridView1.Columns(0)
尝试
如果CB.SelectedItem.ToString=“其他”,则
当TryAgain
OtherGrass=Microsoft.VisualBasic.InputBox(“输入替代植物名称”、“其他植物”、“其他植物”)
如果OtherGrass=“”,则
'将单元格返回到“”
DataGridView1.CurrentRow.Cells(0.Value=“”
出口接头
如果结束
对于i,整数=1到Len(字母)
如果InStr(LCase(OtherGrass),Mid(字母i,1))>0,则
TryAgain=False
退出
如果结束
下一个
对于i As Integer=0到ComboColumn.Items.Count-1
如果LCase(OtherGrass)=LCase(ComboColumn.Items.Item(i).ToString),那么
TryAgain=True
System.Windows.Forms.MessageBox.Show(“此工厂已添加。”)
如果结束
下一个
结束时
'ReDim保留Grasses(Grasses.GetUpperBound(0)+1)
'Grasses(Grasses.GetUpperBound(0))=otherGrasses
ComboColumn.Items.Add(OtherGrass)
如果结束
如果DataGridView1.CurrentRow.Cells(1).Value为零,则
DataGridView1.CurrentRow.Cells(1).Value=txtSpacMult.Text
如果结束
'如果CB.SelectedItem.ToString“”,则
'对于i As Integer=1到DataGridView1.Columns.Count-2
'Dim temp As Boolean=Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index)。单元格(i)。只读
'Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly=False
'temp=Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly
”“接着呢
"完"
EnableRun()
特例
System.Windows.Forms.MessageBox.Show(例如Message)
结束尝试
端接头
而不是:

            ComboColumn.Items.Add(OtherGrass)
付诸表决:


您应该添加并选择新的植物,并将“其他”条目移到末尾。

尝试以下操作:将新文本添加到ComboColumn后,将selectedindex/SleedItem设置为刚才添加的项目。我尝试了添加
CB.selectedindex=ComboColumn.Items.Count-1
,但此时,组合框CB,它具有SelectedIndex方法,没有新项,因此我将获得一个超出范围的异常它最初工作,但是每次将光标传递到组合框上时,我都会得到一个异常(Display和PreferredSize)。当我再次打开组合框时,它没有包含新添加的植物。
            CB.Items.RemoveAt(CB.Items.Count - 1)
            CB.Items.Add(OtherGrass)
            CB.Items.Add("Other")
            CB.SelectedItem = OtherGrass