如何查找dataGrid是否包含一列 [英] How to find if dataGrid contains a column

查看:34
本文介绍了如何查找dataGrid是否包含一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个名为 grid 的现有 DataGrid.

Given an existing DataGrid called grid.

当我尝试访问 grid.Columns("column_name") 时出现异常

When I try to access grid.Columns("column_name") I get an exception

未找到列,列名称

我试过了

If Not IsNull(grid.Columns("column_name")) Then '...

但我仍然得到例外.

我想要一些我可以称之为

I would like something which I could call like

grid.ContainsColumn("column_name")

推荐答案

DataGrid 中的列只有一个标题文本来标识列是什么,因此您可以使用类似的内容来检查列是否存在标题与您要查找的列名称相匹配的列.

The columns in a DataGrid only have a caption text to identify what the column is so you could use something like this to check if the column exists by looking for a column with a Caption that matches the column name you are looking for.

Private Function DataGrid_CheckColumnExists(dataGrid As dataGrid, columnName As String)
    Dim columnCount As Long, columnIndex As Long
    Dim checkColumnName As String

    columnCount = dataGrid.Columns.Count

    For columnIndex = 0 To columnCount - 1
        checkColumnName = dataGrid.Columns(columnIndex).Caption

        DataGrid_CheckColumnExists = (StrComp(checkColumnName, columnName, vbTextCompare) = 0)
        If DataGrid_CheckColumnExists Then Exit Function 'No need to continue once we found it
    Next columnIndex
End Function

这篇关于如何查找dataGrid是否包含一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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