将数据网格视图名称存储在变量中 [英] Storing datagridview name in a variable

查看:59
本文介绍了将数据网格视图名称存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究用户权限.我想加载使用以下代码检查的网格项目.

I am working on user rights. I want to load the grid items checked using following code.

Dim l As Integer = 0, vrGridName As New DataGridView, vrGridItemIndex As Integer
    taSaveTemplates.Connection.ConnectionString += ";password=" & vrSAPWD
    Me.taSaveTemplates.Fill(Me.DsSaveTemplates.tblTemplates, lstTemplateID.Text)
    'Load Grids according to data saved
    Do While DsSaveTemplates.tblTemplates.Rows.Count > l
        vrGridName.Name = DsSaveTemplates.tblTemplates.Rows(l).Item("GridName")
        vrGridItemIndex = DsSaveTemplates.tblTemplates.Rows(l).Item("GridItemIndex")
        vrGridName.Item(0, vrGridItemIndex).Value = True
        l = l + 1
    Loop

vrGridName 存储从 DB 中选择的网格名称,vrGridItemIndex 存储需要检查的项目.

vrGridName stores the name of grid selected from DB and vrGridItemIndex stores the item that needs to be checked.

问题是,当我运行代码时,它说索引是我们的范围.我检查过,vrGridName 不存储网格的名称,而是存储System.windows.datagridview

The problem is, when I run the code, it says Index is our of range. I have checked, the vrGridName does not store the name of grid but stores System.windows.datagridview

请指教.谢谢

推荐答案

您的代码将控件引用视为名称(字符串变量),因此您将获得类型名称 (System.windows.datagridview) 而不是控件的名称.由于模板 DGV 恰好有名称,请使用它:

Your code is treating a control reference as if it were a name (string variable), so you get the Type name (System.windows.datagridview) rather than the name of the control. Since the template DGV happarently has the name, use it:

Dim myGridName As String         ' name is a String, not DGV
Dim myGridItemIndex As Integer
Dim myDGV As DataGridView        ' no NEW - not creating a new one
                                 ' just a reference var
'...
' this is now a For/Each loop
For Each row As DataGridViewRow in DsSaveTemplates.tblTemplates.Rows
    myGridName  = row.Cell("GridName")
    myGridItemIndex  = row.Cell("GridItemIndex")

    ' assuming this code is in a Form:
    ' get a reference to the control
    myDGV = CType(Me.Controls(myGridName), DataGridView)
    ' talk to it like a DGV:
    myDGV.Item(0, myGridItemIndex).Value = True

Next

注意:Option Strict 可能需要对名称和索引进行一些转换

Note: Option Strict would likely require some conversions for the name and index

如果 DGV 位于面板或选项卡等容器控件中,您必须找到"该控件,因为它们将位于那个控件的集合中,而不是表单的集合中.而不是 myDGV = CType(Me.Controls(myGridName), DataGridView):

If the DGV(s) reside in container controls like Panels or Tabs, you have to "find" the control because they will be in that control's collection, not the Form's. Instead of myDGV = CType(Me.Controls(myGridName), DataGridView):

' have the form search for the ctl by name
' the TRUE param tells it to search child controls like panels 
Dim tmpArry = Me.Controls.Find(myGridName, True)

' test for a return 
If tmpArry.Length > 0 Then
    ' tmpArry will be Type Control, so cast it 
    myDGV = CType(tmpArry(0), DataGridView)
End If

这通常从一开始就更好,因此您不必在编码时记住表单的布局.

This is usually better from the start so you do not have to remember how the form is laid out when coding.

这篇关于将数据网格视图名称存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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