在没有特别命名的情况下引用未绑定的DataGridView?

时间:2012-03-15 21:42:05

标签: vb.net visual-studio-2010 datagridview

我使用3个未绑定的DataGridView控件来显示某些信息。为了将信息加载到那些DGV中,我从加密文件中提取信息,解密信息,解析信息,然后尝试用该信息填充DGV。单击菜单项即可调用文件中的加载。以下是我到目前为止的情况:

Private Sub miCLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Handles miCLoad.Click
    Dim FilePath As String = "C:\FList\CList.clt"
    Dim LoadFile As New SaveandLoad.SaveAndLoad
    Dim FileRead As New Simple3Des("MyPassword")
    Dim FileString As String = FileRead.ReadFile(FilePath)


    With LoadFile
        .WhichList = dgCourses
        .FilePath = FilePath
        .DecryptedString = FileRead.DecryptData(FileString)
        .dgList = dgCourses
    End With

    Call LoadFile.LoadFile()
End Sub

Public Class SaveandLoad
Public Property WhichList As New DataGridView
Public Property FilePath As String
Public Property DecryptedString As String
Public Property EncryptedString As String
Public Property dgList As Control

Public Sub LoadFile()

    Dim dgRow As DataGridViewRow
    Dim dgCell As DataGridViewTextBoxCell
    Dim Lines() As String = DecryptedString.Split(vbLf)
    Dim LinesList As List(Of String) = Lines.ToList
    LinesList.RemoveAt(Lines.Length - 1)

    For Each Line As String In LinesList
        Dim Fields() As String = Line.Split(",")
        dgRow = New DataGridViewRow

        For x = 0 To (WhichList.Columns.Count - 1) Step 1
            dgCell = New DataGridViewTextBoxCell
            dgCell.Value = Fields(x).ToString
            dgRow.Cells.Add(dgCell)
        Next
        WhichList.Rows.Add(dgRow)
    Next

    Select Case WhichList.Name
        Case "dgCourses"
            frmFacultyList.dgCourses = WhichList
            frmFacultyList.dgCourses.Refresh()
            WhichList.Dispose()
        Case "dgFList"
            frmFacultyList.dgFList = WhichList
            frmFacultyList.dgFList.Refresh()
            WhichList.Dispose()
        Case "dgSList"
            frmFacultyList.dgSList = WhichList
            frmFacultyList.dgSList.Refresh()
            WhichList.Dispose()
    End Select

    MsgBox("List Successfully Loaded", vbOKOnly, "Load")

End Sub

我希望能够在不使用“select case”或“if-then”语句的情况下引用(或填充)DGV。一旦我开始添加许多其他将在未来添加的DGV,这将效率太低。因此,标题是主要问题。我正在使用VS Express 2010。

2 个答案:

答案 0 :(得分:0)

我不太了解VB,但是,我会在C#中发布我的解决方案(可能会有所帮助......)

DataGridView myDGV;
foreach (var item in this.Controls)
{
    if (item.GetType() == typeof(DataGridView))
    {
        if (((DataGridView)item).Name == WhichList.Name)
        {
            //Cannot assing to 'item' here,  because it is a 'foreach iteration variable'
            //However you can save the variable for later use.
            myDGV = (DataGridView)item;
        }
    }
}
myDGV = WhichList;




// different approach
DataGridView myDGV = (DataGridView)this.Controls.Find(WhichList.Name, false).First();
myDGV = WhichList;

答案 1 :(得分:0)

以下是VB.NET中对我有用的东西:

Dim FormControls As New frmFacultyList.ControlCollection(frmFacultyList)

        For Each DGV As DataGridView In FormControls
            If WhichList.Name = DGV.Name Then
                DGV = WhichList
                DGV.Refresh()
            End If
        Next

创建控件集合的实例,然后使用For Each专门搜索DGV。简单而有效。