Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

vb.net - Storing datagridview name in a variable

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 stores the name of grid selected from DB and vrGridItemIndex stores the item that needs to be checked.

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

Please advise. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...