【问题标题】:Creating a DataGridView Programatically and add clickable cells以编程方式创建 DataGridView 并添加可点击的单元格
【发布时间】:2013-06-19 21:12:33
【问题描述】:

晚上

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

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

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

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:-(

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2015-04-27
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多