0

I am getting my feet wet with VB .Net programming (total novice). I have a DataGridView with amongst other information, a file path to where a particular document is stored. I have added a DataGridViewButtonColumn to the DataGridView, but I cannot figure out how to get the button to open the file.

Sorry, I have no code to provide as starting point for where I get stuck.

Thanks in advance,

Trevor
  • 7,777
  • 6
  • 31
  • 50
Basil
  • 3
  • 4
  • Your question is lacking details. Is the filepath in a particular column? Is there multiple rows with different paths? Please show how you are creating the DataGridView with your data... – Trevor Jan 18 '16 at 12:58
  • My apologies Codexer. Yes, the file path is in a particular column. It is named InvoiceFileLocation. In reference to your second question, Yes, there are multiple rows with different paths. Thank you for responding to my question. – Basil Jan 18 '16 at 13:01
  • There are many tuts/hits when doing a Google search of this. Have you tried any of them? – Trevor Jan 18 '16 at 13:04
  • I have tried searches for DataGridViewButtonColumn and how to open a file using DataGridViewButtonColumn, but I seem to be getting C# answers. I will refine my search based on information I got from your questions above. – Basil Jan 18 '16 at 13:06
  • You can utilize the `CellContentClick` event. Then you can use `Process.Start` and pass your current row/column data (filelath) as the argument for Process.Start – Trevor Jan 18 '16 at 13:07
  • Thank you for your time and the advice, Codexer. I will get back with feedback. – Basil Jan 18 '16 at 13:20
  • Welcome, if you get stuck come back we will be glad to help. – Trevor Jan 18 '16 at 13:22

1 Answers1

0

Sorry I didn't read the post clear enough the first time, and didn't explain my code enough so it got deleted. This uses the contentclick event.

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

You can get rid of most of those lines but I wrote it like that to explain how it worked

myekem
  • 139
  • 8