代码之家  ›  专栏  ›  技术社区  ›  Skitzafreak

使一列DataGridView自动完成字符串数据

  •  1
  • Skitzafreak  · 技术社区  · 7 年前

    我试图在VB.Net中设置一个DataGridView,其中一个名为“Supplier”的列充当带有自动完成源的文本框,以帮助用户输入数据。这是我为实现这一目标而编写的代码:

    Private Sub OrderData_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles orderData.EditingControlShowing
        Dim colIndex = orderData.SelectedCells.Item(0).ColumnIndex
        Dim headerText As String = orderData.Columns(colIndex).HeaderText
        If headerText.Equals("Supplier") Then
            Dim autoText As TextBox = TryCast(e.Control, TextBox)
            If autoText IsNot Nothing Then
                autoText.AutoCompleteMode = AutoCompleteMode.SuggestAppend
                autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
                autoText.AutoCompleteCustomSource = FillTextBoxData("supplier_name", "suppliers")
            End If
        End If
    End Sub
    

    我需要做什么来解决这个问题?

    1 回复  |  直到 7 年前
        1
  •  0
  •   cg7    7 年前

    当你这样做的时候

    Dim headerText As String = orderData.Columns(2).HeaderText
    If headerText.Equals("Supplier") Then
    

    Dim colIndex = orderData.SelectedCells.Item(0).ColumnIndex
    

    然后你可以使用你的代码

    Dim headerText As String = orderData.Columns(colIndex).HeaderText
    

    还添加了一个 Else 第条 If headerText.Equals("Supplier") Then 其他的 autoText.AutoCompleteMode = AutoCompleteMode.None