Results 1 to 5 of 5

Thread: Creating a link for a DGV?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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,
    Last edited by blakemckenna; Jun 21st, 2007 at 03:50 PM.
    Blake

  2. #2
    New Member TirRaven's Avatar
    Join Date
    May 2007
    Posts
    14

    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:
    1. Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    2.         Dim linkColumnIndex As Integer = 3   'or whatever index your column is
    3.         If e.ColumnIndex = linkColumnIndex Then
    4.             System.Diagnostics.Process.Start(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
    5.         End If
    6.     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.)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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?
    Blake

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width