Vb.Net 2005 - Defining a DGV Column as LinkCell???
I am trying to define a column in a DataGridView as a LinkCell and I keep getting the following error:
"Value provided for cell template must of type 'Windows.System.Forms.DataGridViewTextBoxCell' or derive from it"
I've included my code:
Code:
Private Sub FormatGrid3()
Try
grd3.RowHeadersVisible = False
grd3.Columns(0).Width = 100
grd3.Columns(0).HeaderText = "Request ID"
grd3.Columns(1).Width = 140
grd3.Columns(1).CellTemplate = New DataGridViewLinkCell
grd3.Columns(1).HeaderText = "Image Name"
grd3.Columns(2).Width = 430
grd3.Columns(2).HeaderText = "File Path"
Catch ex As Exception
ErrorHandler(strModule, strProcedure, Msg)
End Try
End Sub
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
You can't change the type of a column once it's been created. If you are relying on the grid to create the columns for you then it's going to create text box columns in most cases as that's the default type for most data types. If you want a column of hyperlinks then you'll have to create that column yourself, either in code or, more likely, in the designer. You set the column's DataPropertyName to specify that it should bind to a specific column in the data source.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
Ok,
I got it working...but when I click/doubleclick on the filename (linked cell)...nothing happens. I thought that because it is defined as a LinkCell that it would automatically associate an application to open the link.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
If you had read the MSDN documentation for the DataGridViewLinkColumn class you would already have your answer.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
I did a search using VS Help and all I got was the following:
Code:
Private Sub AddLinkColumn()
Dim links As New DataGridViewLinkColumn()
With links
.HeaderText = ColumnName.ReportsTo.ToString()
.DataPropertyName = ColumnName.ReportsTo.ToString()
.ActiveLinkColor = Color.White
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.TrackVisitedState = True
.VisitedLinkColor = Color.YellowGreen
End With
DataGridView1.Columns.Add(links)
End Sub
This doesn't help me do what I want?
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
Every class has at least two topics dedicated to it in the MSDN library: one an overview of the class and the other a list of members. Had you read the class overview for the DataGridViewLinkColumn class you would have seen this:
Quote:
To respond to user link clicks, handle the DataGridView.CellContentClick event. You can also use the DataGridView.CellClick event to respond to clicks anywhere in the cell.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
If a user clicks on a file name within a LinkCell....will the appropriate program open the file or does that need to be programmed accordingly?
Thanks,
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
Quote:
will the appropriate program open the file
You've already said that when you click a link nothing happens.
Quote:
does that need to be programmed accordingly
The documentation I quoted says that you're supposed to handle the CellContentClick event of the grid. Why do you usually handle events? So that you can execute some code when something happens. In this case that means performing the desired action when the user clicks the content in your link cells.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
you have to pocess the action in event, although you can define a 'LinkCell'. just like in RichText, you can enable LinkText, but you have to process it in LinkClicked event.
you can use default browser to open the link:
Try
System.Diagnostics.Process.Start(e.LinkText)
Catch ex As Exception
MsgBox("Can not open " & e.LinkText & " using default browser!", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
bear
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
Thank you Flying Bear...that's exactly the answer I was looking for. I didn't think my question was that hard to understand. Some people just can't give a straight answer.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
Bear,
To which object does the LinkClicked Event belong to?
Thanks,
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
You handle the CellClick event and test e.ColumnIndex to see it it's the link column. If it is, you do as Flying Bear have shown.
Re: Vb.Net 2005 - Defining a DGV Column as LinkCell???
I didn't think my answer was that hard to understand:
Quote:
handle the CellContentClick event of the grid