Creating a link for a DGV?
I have a DGV in which one of the columns is a DB Table field called fileName. It's values look like this: "Invoice.txt" (minus the quotes).
I wish to be able to make it a link...where the user can click on it and the document will open according to it's association if one is established. How can I get the fileName field to load in the grid as a HyperLink or Link?
Thanks,
Re: Creating a link for a DGV?
in your data grid view, make the fileName column of type DataGridViewLinkColumn instead of the default DataGridViewTextBoxColumn. This will underline the text and change the cursor when you hover over the cell.
Then to open the file in the link, in the datagridviews cellClick event:
VB Code:
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim linkColumnIndex As Integer = 3 'or whatever index your column is
If e.ColumnIndex = linkColumnIndex Then
System.Diagnostics.Process.Start(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
End If
End Sub
(this is assuming that the full path is in cell being clicked. If it is not, you will have to append the folder path to the the cell value.)
Re: Creating a link for a DGV?
You should handle the CellContentClick event rather than the CellClick. Otherwise you will be acting even if the link wasn't activated.
Note also that you must add this column at design time if your grid is generating the columns for you. Otherwise you will just get the text box column as normal. You set the column's DataPropertyName to tell it which column of the data source to bind to.
Re: Creating a link for a DGV?
JMC,
This grid is created as a result of a DB Table. In this case there are only 3 columns. In design view, the grid is blank but I bind the grid to a DB Table. Once I bind the grid to the Table, I have a sub that formats the headers. Could I add the code there to create the linked column?
Re: Creating a link for a DGV?
Quote:
Originally Posted by blakemckenna
JMC,
This grid is created as a result of a DB Table. In this case there are only 3 columns. In design view, the grid is blank but I bind the grid to a DB Table. Once I bind the grid to the Table, I have a sub that formats the headers. Could I add the code there to create the linked column?
I specifically addressed that issue in my previous post.
Quote:
Originally Posted by jmcilhinney
Note also that you must add this column at design time if your grid is generating the columns for you. Otherwise you will just get the text box column as normal. You set the column's DataPropertyName to tell it which column of the data source to bind to.