仅针对单列在 datagridview 中自动完成文本 [英] Auto-complete text in the datagridview for single column only

查看:21
本文介绍了仅针对单列在 datagridview 中自动完成文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 datagridview 中提供自动完成文本.但是,我在所有列中都获得了自动完成文本,并且在单击第三列(带有组合框)后,应用程序开始在编辑其他单元格时显示错误.

I tried to provide auto-complete text in datagridview. But, I got auto-complete text in all column and after clicking on third column (with combobox) application start displaying error on editing other cells.

我希望在第一列中自动完成文本框,即仅名称".我无法管理代码,因此自动完成不会显示在第二列中,即年龄",并且点击组合框列后也不会发生错误.

代码如下.

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

推荐答案

在 Else 子句中做一个测试:

Do a test in Else clause as :

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

这篇关于仅针对单列在 datagridview 中自动完成文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆