事件上的Openfile

时间:2016-01-18 12:45:04

标签: vb.net datagridview datagridviewbuttoncolumn

我正熟悉VB .Net编程(总新手)。我有一个DataGridView以及其他信息,一个存储特定文档的文件路径。我已经将DataGridViewButtonColumn添加到DataGridView,但我无法弄清楚如何让按钮打开文件。

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

提前致谢,

1 个答案:

答案 0 :(得分: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

你可以摆脱大部分这些线,但我这样写它来解释它是如何工作的