0

我正在使用 VB .Net 编程(完全是新手)。我有一个 DataGridView,其中包含其他信息,一个特定文档存储位置的文件路径。我已向 DataGridView 添加了一个 DataGridViewButtonColumn,但我无法弄清楚如何让按钮打开文件。

抱歉,我没有代码可以作为我卡住的起点。

提前致谢,

4

1 回答 1

0

对不起,我第一次看帖子不够清楚,也没有充分解释我的代码,所以它被删除了。这使用 contentclick 事件。

Dim Filetext As String = "" 'At start of the class to make it available to the whole class

Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim FilePathColumn As Integer = 0 'File path is in Column 0
Dim ButtonColumn As Integer = 1 'Column buttons are in
Dim RowClicked As Integer = e.RowIndex 'This gets the row that you clicked the button in

If e.ColumnIndex = ButtonColumn Then 'Make sure you clicked a button
    Dim FILE_PATH As String = DataGridView1.Rows(RowClicked).Cells(FilePathColumn).ToString 'Get the path to the file
    If System.IO.File.Exists(FILE_PATH) Then 'Make sure file exists
        Filetext = System.IO.File.ReadAllText(FILE_PATH) 'Save file to a variable

        'OR

        Process.Start(FILE_PATH) 'To open it
    End If
End If
End Sub

你可以去掉大部分这些行,但我这样写是为了解释它是如何工作的

于 2016-01-18T13:32:55.560 回答