0

晚上

我正在努力实现我的目标!简而言之,我正在使用 Google Drive API 在 VB.net 中创建一个应用程序,允许您查看/下载文件等。

基本上,我计划为我拥有的不同云提供商使用几个不同的 api。我必须达到的阶段是我有我的文件集合和列表中的各种属性。在加载时,我正在检查是否已将 google 帐户添加到我的程序中,如果是,则在 tabcontrol 上创建新标签页,然后创建 datagridview 并将其添加到新标签页上的集合中。这工作正常,并按原样显示我的所有数据。

我想要实现的是在我的下载列中添加一个可点击的链接,而不是显示 url。在将其转换为 vb.net 后,我​​一直在尝试操作C# DataGridViewLinkCell 显示在此处找到的代码。这基本上是我最终的结果:

If My.Settings.GoogleClientID <> "" Then
            Dim GD As New Properties.GDrive
            GD.APIClientID = My.Settings.GoogleClientID
            GD.APIClientSecret = My.Settings.GoogleClientSecret

            clsDrive.GD = GD
            clsDrive.GetSpace()
            clsDrive.RefreshFiles()
            Dim GoogleDriveTab As New TabPage
            GoogleDriveTab.Text = "Google Drive"
            tc_CloudManager.TabPages.Add(GoogleDriveTab)

            Dim GoogleDriveDGV As New DataGridView
            GoogleDriveDGV.Name = "GoogleDriveDGV"
            GoogleDriveDGV.Dock = DockStyle.Fill
            GoogleDriveDGV.Columns.Add("FileTitle", "File Title")
            GoogleDriveDGV.Columns.Add("FileExtension", "File Extension")
            GoogleDriveDGV.Columns.Add("FileSize", "File Size")
            Dim dgvlc As New DataGridViewLinkColumn()
            dgvlc.ActiveLinkColor = Color.Blue
            dgvlc.HeaderText = "Download"
            GoogleDriveDGV.Columns.Add(dgvlc)
            GoogleDriveDGV.RowHeadersVisible = False
            Dim c As New customcolumn()
            GoogleDriveDGV.Columns.Add(c)

            For i As Integer = 0 To GoogleDriveDGV.Columns.Count - 1
                GoogleDriveDGV.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
            Try
                For Each DriveFile In GD.DriveFiles
                    Dim row As DataGridViewRow = DirectCast(GoogleDriveDGV.Rows(0).Clone, DataGridViewRow)
                    Dim title As String = DriveFile.Title
                    If title.Length > 60 Then
                        title = title.Substring(0, 60)
                    End If

                    Dim fs As Integer = DriveFile.FileSize
                    Dim fsf As String
                    If fs > 1048576 Then
                        fsf = Math.Round(fs / 1048576, 2) & " MB"
                    Else
                        fsf = fs & " Bytes"
                    End If
                    Dim fe As String
                    If DriveFile.FileExtension = "" Then
                        fe = "Folder"
                    Else
                        fe = DriveFile.FileExtension
                    End If

                    row.Cells(0).Value = title
                    row.Cells(1).Value = fe
                    row.Cells(2).Value = fsf
                    row.Cells(3).Value = "Download File"
                    DirectCast(GoogleDriveDGV.Columns(3), customcolumn).urls.Add(3, DriveFile.DownloadUrl)
                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            GoogleDriveTab.Controls.Add(GoogleDriveDGV)
        End If
    End Sub
    Class customcolumn
        Inherits System.Windows.Forms.DataGridViewLinkColumn
        Public urls As New Dictionary(Of Integer, String)()
    End Class

    Private Sub GoogleDriveDGV_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        For Each url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls
            If url.Key = e.RowIndex Then
                Process.Start(url.Value)
                Exit For
            End If
        Next
    End Sub

我有两个我无法弄清楚的问题:

1) GoogleDriveDGV 未在此处声明:对于每个 url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls

2) 如果我注释掉 GoogleDriveDGV_CellContentClick 子并尝试填充 datagridview 我得到 System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.DataGridViewLinkColumn' to type 'customcolumn' here DirectCast(GoogleDriveDGV.Columns(3 ), customcolumn).urls.Add(3, DriveFile.DownloadUrl)

谁能让我知道我想要实现的目标是否可行以及任何提示?

更新!!!

我现在有这个:

If My.Settings.GoogleClientID <> "" Then
            Dim GD As New Properties.GDrive
            GD.APIClientID = My.Settings.GoogleClientID
            GD.APIClientSecret = My.Settings.GoogleClientSecret

            clsDrive.GD = GD
            clsDrive.GetSpace()
            clsDrive.RefreshFiles()


            Dim GoogleDriveTab As New TabPage
            GoogleDriveTab.Text = "Google Drive"
            tc_CloudManager.TabPages.Add(GoogleDriveTab)

            Dim GoogleDriveDGV As New DataGridView
            GoogleDriveDGV.Name = "GoogleDriveDGV"
            GoogleDriveDGV.Dock = DockStyle.Fill
            GoogleDriveDGV.Columns.Add("FileTitle", "File Title")
            GoogleDriveDGV.Columns.Add("FileExtension", "File Extension")
            GoogleDriveDGV.Columns.Add("FileSize", "File Size")
            Dim c As New customcolumn()
            GoogleDriveDGV.Columns.Add(c)
            GoogleDriveDGV.RowHeadersVisible = False

            For i As Integer = 0 To GoogleDriveDGV.Columns.Count - 1
                GoogleDriveDGV.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
            Dim trow As Integer = 0
            Try
                For Each DriveFile In GD.DriveFiles
                    Dim row As DataGridViewRow = DirectCast(GoogleDriveDGV.Rows(0).Clone, DataGridViewRow)
                    Dim title As String = DriveFile.Title
                    If title.Length > 60 Then
                        title = title.Substring(0, 60)
                    End If

                    Dim fs As Integer = DriveFile.FileSize
                    Dim fsf As String
                    If fs > 1048576 Then
                        fsf = Math.Round(fs / 1048576, 2) & " MB"
                    Else
                        fsf = fs & " Bytes"
                    End If
                    Dim fe As String
                    If DriveFile.FileExtension = "" Then
                        fe = "Folder"
                    Else
                        fe = DriveFile.FileExtension
                    End If

                    row.Cells(0).Value = title
                    row.Cells(1).Value = fe
                    row.Cells(2).Value = fsf
                    row.Cells(3).Value = "Download"

                    DirectCast(GoogleDriveDGV.Columns(3), customcolumn).urls.Add(trow, DriveFile.DownloadUrl)
                    GoogleDriveDGV.Rows.Add(row)
                    trow = trow + 1

                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            GoogleDriveTab.Controls.Add(GoogleDriveDGV)
        End If
    End Sub


    Class customcolumn
        Inherits System.Windows.Forms.DataGridViewLinkColumn
        Public urls As New Dictionary(Of Integer, String)()
    End Class


    'Private Sub GoogleDriveDGV_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
    '    For Each url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls
    '        If url.Key = e.RowIndex Then
    '            Process.Start(url.Value)
    '            Exit For
    '        End If
    '    Next
    'End Sub

几乎就在那里,我现在有充满项目和下载链接的datagridview,只是无法解决上面的问题1 :-(

4

1 回答 1

1

你会相信我解决了:-)

基本上我需要添加一个处理程序。

完整代码:

 Private Sub CloudManager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If My.Settings.GoogleClientID <> "" Then
            Dim GD As New Properties.GDrive
            GD.APIClientID = My.Settings.GoogleClientID
            GD.APIClientSecret = My.Settings.GoogleClientSecret

            clsDrive.GD = GD
            clsDrive.GetSpace()
            clsDrive.RefreshFiles()


            Dim GoogleDriveTab As New TabPage
            GoogleDriveTab.Text = "Google Drive"
            tc_CloudManager.TabPages.Add(GoogleDriveTab)

            Dim GoogleDriveDGV As New DataGridView

            GoogleDriveDGV.Name = "GoogleDriveDGV"
            GoogleDriveDGV.Dock = DockStyle.Fill
            GoogleDriveDGV.Columns.Add("FileTitle", "File Title")
            GoogleDriveDGV.Columns.Add("FileExtension", "File Extension")
            GoogleDriveDGV.Columns.Add("FileSize", "File Size")
            Dim c As New customcolumn()
            GoogleDriveDGV.Columns.Add(c)
            AddHandler GoogleDriveDGV.CellContentClick, AddressOf GoogleDriveDGV_CellContentClick
            GoogleDriveDGV.RowHeadersVisible = False

            For i As Integer = 0 To GoogleDriveDGV.Columns.Count - 1
                GoogleDriveDGV.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
            Dim trow As Integer = 0
            Try
                For Each DriveFile In GD.DriveFiles
                    Dim row As DataGridViewRow = DirectCast(GoogleDriveDGV.Rows(0).Clone, DataGridViewRow)
                    Dim title As String = DriveFile.Title
                    If title.Length > 60 Then
                        title = title.Substring(0, 60)
                    End If

                    Dim fs As Integer = DriveFile.FileSize
                    Dim fsf As String
                    If fs > 1048576 Then
                        fsf = Math.Round(fs / 1048576, 2) & " MB"
                    Else
                        fsf = fs & " Bytes"
                    End If
                    Dim fe As String
                    If DriveFile.FileExtension = "" Then
                        fe = "Folder"
                    Else
                        fe = DriveFile.FileExtension
                    End If

                    row.Cells(0).Value = title
                    row.Cells(1).Value = fe
                    row.Cells(2).Value = fsf
                    row.Cells(3).Value = "Download"

                    DirectCast(GoogleDriveDGV.Columns(3), customcolumn).urls.Add(trow, DriveFile.DownloadUrl)
                    GoogleDriveDGV.Rows.Add(row)
                    trow = trow + 1

                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            GoogleDriveTab.Controls.Add(GoogleDriveDGV)
        End If
    End Sub


    Class customcolumn
        Inherits System.Windows.Forms.DataGridViewLinkColumn
        Public urls As New Dictionary(Of Integer, String)()
    End Class


    Private Sub GoogleDriveDGV_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        For Each url As KeyValuePair(Of Integer, String) In DirectCast(sender.Columns(e.ColumnIndex), customcolumn).urls
            If url.Key = e.RowIndex Then
                Process.Start(url.Value)
                Exit For
            End If
        Next
    End Sub


End Class
于 2013-06-19T22:03:01.373 回答