datagridview上的自动完成单元格

时间:2016-03-02 03:12:06

标签: vb.net datagridview autocomplete

我试图通过代码

在datagridview中自动填充单元格
Private Sub DtgFilter_EditingControlShowing1(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DtgFilter.EditingControlShowing If DtgFilter.CurrentCell.ColumnIndex <> 1 Then
             If DtgFilter.CurrentCell.ColumnIndex = 1 Then
               Try
                With DirectCast(e.Control, TextBox)
                    .AutoCompleteMode = AutoCompleteMode.Suggest
                    .AutoCompleteSource = AutoCompleteSource.CustomSource
                    Dim TxtData As New AutoCompleteStringCollection()
                    getData(TxtData)
                    .AutoCompleteCustomSource = TxtData
                End With    
            Catch ex As Exception
                MsgBox("Fault!")
            End Try
        End If
    End Sub

这是 Getdata() sub

Private Sub getData(ByVal dataCollection As AutoCompleteStringCollection)

        Dim strSQL As String = ""           
        Dim DatDS As New DataSet
        Dim AccNo As String

        AccNo = TxtAccno.Text
        strSQL = "select AccNo from Account where Accno like '%" & Accno & "%' "

        Dim SqlData As New SqlDataAdapter(strSQL, ClsDatabase.SqlCon)
        SqlData.Fill(DatDS)
        For Each row As DataRow In DatDS.Tables(0).Rows
            dataCollection.Add(row(0).ToString())
        Next

    End Sub

我希望它只自动填充列&#34; 1&#34;在datagridview中由代码中的这一行

If DtgFilter.CurrentCell.ColumnIndex = 1 Then

但它实际上影响了datagridview中的所有列。

1 个答案:

答案 0 :(得分:0)

以下是我在appgridview_editingcontrolshowing事件中的应用:

Dim itemcode As TextBox = TryCast(e.Control, TextBox)
Dim reader As SqlDataReader
Dim x As New AutoCompleteStringCollection
If grdracuni.CurrentCell.ColumnIndex = 3 Then
Sqlconnection1.open()
reader=sqlcommand1.ExecuteReader
While reader.read
x.add(reader.getvalue())
End While
reader.Close()
Sqlconnection1.close()
If itemcode IsNot Nothing Then
                itemcode.AutoCompleteMode = AutoCompleteMode.SuggestAppend
                itemcode.AutoCompleteCustomSource = x
                itemcode.AutoCompleteSource = AutoCompleteSource.CustomSource
End If

End If