我尝试在datagridview中提供自动完成的文本。但是,我在所有列中都获得了自动完成文本,在单击第三列(使用组合框)后,应用程序开始在编辑其他单元格时显示错误。
我希望第一列中的自动填写文本框,即仅“名称” 。我无法管理代码,因此自动完成不会显示在第二列,即“年龄”,并且在点击组合框列后也没有出现错误。
代码如下。
Public Class Form1
Private Sub appData(ByVal data As AutoCompleteStringCollection, ByVal c As String)
data.Add("Ravi")
data.Add("Raj")
data.Add("Raja")
data.Add("r " & c)
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Try
MsgBox(e.Control.ToString)
Dim header As String = DataGridView1.Columns(0).HeaderText
If TypeOf e.Control Is TextBox AndAlso header.Equals("Name") AndAlso DataGridView1.CurrentCell.ColumnIndex = 0 Then
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim text As TextBox = TryCast(e.Control, TextBox)
If text IsNot Nothing Then
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data
End If
Else
Dim text As TextBox = TryCast(e.Control, TextBox)
text.AutoCompleteCustomSource = Nothing
text.AutoCompleteSource = AutoCompleteSource.None
text.AutoCompleteMode = AutoCompleteMode.None
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("ColName", "Name")
DataGridView1.Columns.Add("ColAge", "Age")
Dim ComCol As New DataGridViewComboBoxColumn
ComCol.Items.Add(1)
ComCol.Items.Add(2)
ComCol.HeaderText = "Combobox Col"
DataGridView1.Columns.Add(ComCol)
End Sub
End Class
答案 0 :(得分:1)
仅使用:
If header.Equals("Name") Then
而不是:
If TypeOf e.Control Is TextBox AndAlso header.Equals("Name") AndAlso
DataGridView1.CurrentCell.ColumnIndex = 0 Then
答案 1 :(得分:1)
在Else子句中进行测试:
If header.Equals("Name") Then
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim text As TextBox = TryCast(e.Control, TextBox)
If text IsNot Nothing Then
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data
End If
ElseIf TypeOf e.Control Is TextBox Then
Dim text As TextBox = TryCast(e.Control, TextBox)
text.AutoCompleteCustomSource = Nothing
text.AutoCompleteSource = AutoCompleteSource.None
text.AutoCompleteMode = AutoCompleteMode.None
End If
End If
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
Try
Catch ex As Exception
End Try
End Sub
答案 2 :(得分:0)
我认为问题在于你的DataGridView1_EditingControlShowing比你意识到的更频繁。
也许您应该只对表单加载事件执行自动完成工作?这样,它只需运行一次......
自动完成工作'我正在谈论,成为这一部分:
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data