繁体   English   中英

引用未绑定的DataGridView而不进行特定命名?

[英]Referencing an Unbound DataGridView Without Specifically Naming It?

我正在使用3个未绑定的DataGridView控件来显示某些信息。 要将信息加载到这些DGV中,我正在从加密文件中提取信息,对其进行解密,解析信息,然后尝试用该信息填充DGV。 通过菜单项click调用从文件中加载。 这是我到目前为止有:

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

我希望能够引用(或填写)DGV,而无需使用“选择大小写”或“ if-then”语句。 一旦我开始添加许多其他DGV(将来会添加),这将太低效。 因此,标题是主要问题。 我正在使用VS Express 2010。

我不太了解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;

这是在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。 简单高效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM