vb.net 以编程方式创建 DataGridView 并添加可点击的单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17201438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:58:39  来源:igfitidea点击:

Creating a DataGridView Programatically and add clickable cells

vb.netvisual-studio-2010datagridviewdatagridviewlinkcolumn

提问by dave

Evening

晚上

I am struggling to achieve what I have set out todo! In a nutshell I am working with the Google Drive API to create an app in VB.net that allows you to view / download files etc.

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

Basically I am planning on using a couple of different api's for the different cloud provider s I have. The stage I have got to is that I have my collection of files and there various properties in a list. On load I am checking to see if a google account has been added to my program and if so creating a new tabpage on a tabcontrol, I then create the datagridview and add it to the collection on the new tab page. This works fine and displays all my data as is.

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

What I am trying to achieve is adding a clickable link to my download column instead of displaying the url. I have been trying to manipulate the code found here C# DataGridViewLinkCell Displayafter converting it to vb.net. This is basically what I have ended up with:

我想要实现的是在我的下载列中添加一个可点击的链接,而不是显示 url。在将其转换为 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

I have two problems that I cant figure out:

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

1) GoogleDriveDGV is not declared here : For Each url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls

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

2) If I comment out the GoogleDriveDGV_CellContentClick sub and just try to populate the datagridview I get 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)

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)

Can anyone let me know if what I am trying to achieve is possible and any hints?

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

Update!!!

更新!!!

I now have this:

我现在有这个:

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

Which is almost there, I now have the datagridview filled with the items and a download link, just cant work out how to solve issue 1 above :-(

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

回答by dave

Would you believe it I worked it out :-)

你相信我解决了它:-)

Basically I needed to add a handler.

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

Full code:

完整代码:

 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