Victor Charles
asked on
Hrlp with making image cells in DataGridView look like hyperlinks using VB.NET
Hi,
How do I make an image cell in my DataGridView look like a hyperlink so users know they can click on it to see the image.
Thanks,
Victor
How do I make an image cell in my DataGridView look like a hyperlink so users know they can click on it to see the image.
Thanks,
Victor
One option is to add text on header column such as "click to enlarge". Other option, though requires more work, is to add a small indication to the image itself (such as drawing a hand icon in corner of the image) on the fly when rendering.
ASKER
Hi,
Thank you for your comments and the link, I will first try to solution from the link from the first post.
Victor
Thank you for your comments and the link, I will first try to solution from the link from the first post.
Victor
Try this ..
You have already a column with the images files
Add a new LinkColumn ->Copy the "Values" from the Images Column to the new Link Column and then make the first images Column invisible.(Visible=False)
And then use the LinkColumn Index for the CellContentClick
So after populating the DatagridView
Yiannis
You have already a column with the images files
Add a new LinkColumn ->Copy the "Values" from the Images Column to the new Link Column and then make the first images Column invisible.(Visible=False)
And then use the LinkColumn Index for the CellContentClick
So after populating the DatagridView
Dim links As New DataGridViewLinkColumn()
With links
.Name = "Link"
.HeaderText = "Image"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
DataGridView1.Columns.Insert(Me.DataGridView1.ColumnCount, links)
Dim row As DataGridViewRow
For Each row In Me.DataGridView1.Rows
row.Cells(Me.DataGridView1.Columns.Count - 1).Value = row.Cells("MyImageFileColumn").Value
Next
End Sub
Yiannis
ASKER
Hi,
I just read your post, was just about to send you the message below, not sure if I will still have the same issue, will try your approach and get back to you later.
Thanks,
V.
Message intended prior to readind your post:
Hi,
I used Imports System.Windows.Forms.DataG ridViewLin kColumn
And
Private Sub AddLinkColumn()
Dim links As New DataGridViewLinkColumn()
With links
.UseColumnTextForLinkValue = True
.HeaderText = ColumnName.ReportsTo.ToStr ing()
.DataPropertyName = ColumnName.ReportsTo.ToStr ing()
.ActiveLinkColor = Color.White
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.TrackVisitedState = True
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Add(link s)
End Sub
But I received errors on ColumName (Not declared) and Linkbehavior (ambegious). I am trying to set a link to column 6 of C1Screen1. How do I fix these errors?
Thanks,
Victor
I just read your post, was just about to send you the message below, not sure if I will still have the same issue, will try your approach and get back to you later.
Thanks,
V.
Message intended prior to readind your post:
Hi,
I used Imports System.Windows.Forms.DataG
And
Private Sub AddLinkColumn()
Dim links As New DataGridViewLinkColumn()
With links
.UseColumnTextForLinkValue
.HeaderText = ColumnName.ReportsTo.ToStr
.DataPropertyName = ColumnName.ReportsTo.ToStr
.ActiveLinkColor = Color.White
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.TrackVisitedState = True
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Add(link
End Sub
But I received errors on ColumName (Not declared) and Linkbehavior (ambegious). I am trying to set a link to column 6 of C1Screen1. How do I fix these errors?
Thanks,
Victor
try the code i post you .
ASKER
Hi,
I tried your code, but when I click on the link, the image does not appear. Below is the code I used in the cell click event to see the image. How do I modify it to see the image when I click on the new colum containing the links? Also, is it possible to see only the image name in the new column instead of the full path?
Private Sub C1Screen1_CellClick1(sende r As Object, e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles C1Screen1.CellClick
Dim imgStr As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns("Imag e").Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "Image").V alue
imgStrAA = Me.C1Screen1.Rows(e.RowInd ex).Cells( 1).Value
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
End Sub
Thanks,
Victor
I tried your code, but when I click on the link, the image does not appear. Below is the code I used in the cell click event to see the image. How do I modify it to see the image when I click on the new colum containing the links? Also, is it possible to see only the image name in the new column instead of the full path?
Private Sub C1Screen1_CellClick1(sende
Dim imgStr As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns("Imag
imgStr = Me.C1Screen1.Rows(e.RowInd
imgStrAA = Me.C1Screen1.Rows(e.RowInd
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
End Sub
Thanks,
Victor
Based on information in question, I assumed that image is already clickable but you need to indicate that to user. Is that not right? Is the image not clickable currently?
change
If e.ColumnIndex = Me.C1Screen1.Columns("Imag e").Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "Image").V alue\
With
If e.ColumnIndex = Me.C1Screen1.Columns("Link ").Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "Link").Va lue
If e.ColumnIndex = Me.C1Screen1.Columns("Imag
imgStr = Me.C1Screen1.Rows(e.RowInd
With
If e.ColumnIndex = Me.C1Screen1.Columns("Link
imgStr = Me.C1Screen1.Rows(e.RowInd
@CodeCruiser
These are not Images but Path Actually..
These are not Images but Path Actually..
ASKER
Hi Codecruiser,
There are no images in the image colum, just the image's filename.
John,
The last change did not work, when I click on the link I still don't see the image.
would it be easier to keep the original Image colum and somehow change its text color to blue and underline the text? That way we can use the same code that worked before.
Thanks,
Victor
There are no images in the image colum, just the image's filename.
John,
The last change did not work, when I click on the link I still don't see the image.
would it be easier to keep the original Image colum and somehow change its text color to blue and underline the text? That way we can use the same code that worked before.
Thanks,
Victor
can you post your code?
ASKER
Hi,
I'm sorry for the late reply, just got home, will get back to you with the code shortly.
Victor
I'm sorry for the late reply, just got home, will get back to you with the code shortly.
Victor
ASKER
Hi,
Below is the code I am using.
Button click event.
'new link column
Dim links As New DataGridViewLinkColumn()
With links
.Name = "Link"
.HeaderText = "Image"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Insert(M e.C1Screen 1.ColumnCo unt, links)
Dim row As DataGridViewRow
For Each row In Me.C1Screen1.Rows
row.Cells(Me.C1Screen1.Col umns.Count - 1).Value = row.Cells("Image").Value
Next
'************************* ********** ********** ********** ********** ********** ********** *****
Private Sub C1Screen1_CellClick1(sende r As Object, e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles C1Screen1.CellClick
Dim imgStr As String = Nothing
' If e.ColumnIndex = Me.C1Screen1.Columns("Imag e").Index Then
'imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "Image").V alue
If e.ColumnIndex = Me.C1Screen1.Columns("Link ").Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "Link").Va lue
imgStrAA = Me.C1Screen1.Rows(e.RowInd ex).Cells( 1).Value
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
End Sub
Below is the code I am using.
Button click event.
'new link column
Dim links As New DataGridViewLinkColumn()
With links
.Name = "Link"
.HeaderText = "Image"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Insert(M
Dim row As DataGridViewRow
For Each row In Me.C1Screen1.Rows
row.Cells(Me.C1Screen1.Col
Next
'*************************
Private Sub C1Screen1_CellClick1(sende
Dim imgStr As String = Nothing
' If e.ColumnIndex = Me.C1Screen1.Columns("Imag
'imgStr = Me.C1Screen1.Rows(e.RowInd
If e.ColumnIndex = Me.C1Screen1.Columns("Link
imgStr = Me.C1Screen1.Rows(e.RowInd
imgStrAA = Me.C1Screen1.Rows(e.RowInd
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
It works! Is it still possible to only show the image's file name instead of the entire path?
Thank You!
Victor
It works! Is it still possible to only show the image's file name instead of the entire path?
Thank You!
Victor
yes it is but you are already have this piece of code..adjust it ..
ASKER
Ok, I will work on that part.
Thank You.
Victor
Thank You.
Victor
Glad Victor...
ASKER
Hi,
I removed the path with the following code.
row.Cells(Me.C1Screen1.Col umns.Count - 1).Value = Path.GetFileName(row.Cells ("Image"). Value.ToSt ring).
I will post another related question, just for addtional knowledge using another approach with your previous solution.
Is it possible to change the mouse icon when you place the cursor on a particular column
to make it appear as a link and to change the text color and underline the text?
Thanks,
V.
I removed the path with the following code.
row.Cells(Me.C1Screen1.Col
I will post another related question, just for addtional knowledge using another approach with your previous solution.
Is it possible to change the mouse icon when you place the cursor on a particular column
to make it appear as a link and to change the text color and underline the text?
Thanks,
V.
Yes i think you can .....I got it...
ASKER
Hi John,
I am trying the code below for another image column in C1Scree1 but now the link works for the ImageA column but not the Image column. Can you please help me fix the code two work with another Image column.
Button Click event:
Dim links As New DataGridViewLinkColumn()
With links
.Name = "Link"
.HeaderText = "Image"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Insert(M e.C1Screen 1.ColumnCo unt, links)
C1Screen1.Columns("Image") .Visible = False '<-This is the Column With the Paths.The initial one
Dim row As DataGridViewRow
For Each row In Me.C1Screen1.Rows
' row.Cells(Me.C1Screen1.Col umns.Count - 1).Value = row.Cells("Image").Value
row.Cells(Me.C1Screen1.Col umns.Count - 1).Value = Path.GetFileName(row.Cells ("Image"). Value.ToSt ring)
Next
'************************* ********** ********
'************************* ********** ********** *******
Dim linksA As New DataGridViewLinkColumn()
With linksA
.Name = "LinkA"
.HeaderText = "ImageA"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Insert(M e.C1Screen 1.ColumnCo unt, linksA)
C1Screen1.Columns("ImageA" ).Visible = False '<-This is the Column With the Paths.The initial one
Dim rowA As DataGridViewRow
For Each rowA In Me.C1Screen1.Rows
' rowA.Cells(Me.C1Screen1.Co lumns.Coun t - 1).Value = rowA.Cells("Image").Value
rowA.Cells(Me.C1Screen1.Co lumns.Coun t - 1).Value = Path.GetFileName(rowA.Cell s("ImageA" ).Value.To String)
Next
'************************* ********** *
Private Sub C1Screen1_CellClick1(sende r As Object, e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles C1Screen1.CellClick
Dim imgStr As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns(Me.C1 Screen1.Co lumns.Coun t - 1).Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( Me.C1Scree n1.Columns .Count - 1).Value
imgStrAA = Me.C1Screen1.Rows(e.RowInd ex).Cells( 1).Value
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
Dim imgStrA As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns(Me.C1 Screen1.Co lumns.Coun t - 1).Index Then
imgStrA = Me.C1Screen1.Rows(e.RowInd ex).Cells( Me.C1Scree n1.Columns .Count - 1).Value
imgStrAA = Me.C1Screen1.Rows(e.RowInd ex).Cells( 1).Value
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStrA)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
End Sub
Thanks,
Victor
I am trying the code below for another image column in C1Scree1 but now the link works for the ImageA column but not the Image column. Can you please help me fix the code two work with another Image column.
Button Click event:
Dim links As New DataGridViewLinkColumn()
With links
.Name = "Link"
.HeaderText = "Image"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Insert(M
C1Screen1.Columns("Image")
Dim row As DataGridViewRow
For Each row In Me.C1Screen1.Rows
' row.Cells(Me.C1Screen1.Col
row.Cells(Me.C1Screen1.Col
Next
'*************************
'*************************
Dim linksA As New DataGridViewLinkColumn()
With linksA
.Name = "LinkA"
.HeaderText = "ImageA"
.ActiveLinkColor = Color.Red
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.VisitedLinkColor = Color.YellowGreen
End With
C1Screen1.Columns.Insert(M
C1Screen1.Columns("ImageA"
Dim rowA As DataGridViewRow
For Each rowA In Me.C1Screen1.Rows
' rowA.Cells(Me.C1Screen1.Co
rowA.Cells(Me.C1Screen1.Co
Next
'*************************
Private Sub C1Screen1_CellClick1(sende
Dim imgStr As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns(Me.C1
imgStr = Me.C1Screen1.Rows(e.RowInd
imgStrAA = Me.C1Screen1.Rows(e.RowInd
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
Dim imgStrA As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns(Me.C1
imgStrA = Me.C1Screen1.Rows(e.RowInd
imgStrAA = Me.C1Screen1.Rows(e.RowInd
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStrA)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
End Sub
Thanks,
Victor
you did not change the datagridview you want to add the Column
C1Screen1.Columns.Insert(M e.C1Screen 1.ColumnCo unt, links)
C1Screen1.Columns.Insert(M e.C1Screen 1.ColumnCo unt, linksA)
You add it in the same...
C1Screen1.Columns.Insert(M
C1Screen1.Columns.Insert(M
You add it in the same...
ASKER
John,
Both (Image, ImageA) columns are in C1Screen1. Below is the code without the link that worked.
Button Click:
Dim ImageRow As DataGridViewRow
For Each ImageRow In Me.C1Screen1.Rows
If Not ImageRow.IsNewRow Then
ImageRow.Cells(9).Value = Path.GetFileName(ImageRow. Cells(9).V alue.ToStr ing)
End If
Next
For Each ImageRow In Me.C1Screen1.Rows
If Not ImageRow.IsNewRow Then
ImageRow.Cells(10).Value = Path.GetFileName(ImageRow. Cells(10). Value.ToSt ring)
End If
Next
CellClick:
'Dim imgStr As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns("Imag e").Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "Image").V alue
imgStrAA = Me.C1Screen1.Rows(e.RowInd ex).Cells( 1).Value
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
If e.ColumnIndex = Me.C1Screen1.Columns("Imag eA").Index Then
imgStr = Me.C1Screen1.Rows(e.RowInd ex).Cells( "ImageA"). Value
imgStrAA = Me.C1Screen1.Rows(e.RowInd ex).Cells( 1).Value
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
Both (Image, ImageA) columns are in C1Screen1. Below is the code without the link that worked.
Button Click:
Dim ImageRow As DataGridViewRow
For Each ImageRow In Me.C1Screen1.Rows
If Not ImageRow.IsNewRow Then
ImageRow.Cells(9).Value = Path.GetFileName(ImageRow.
End If
Next
For Each ImageRow In Me.C1Screen1.Rows
If Not ImageRow.IsNewRow Then
ImageRow.Cells(10).Value = Path.GetFileName(ImageRow.
End If
Next
CellClick:
'Dim imgStr As String = Nothing
If e.ColumnIndex = Me.C1Screen1.Columns("Imag
imgStr = Me.C1Screen1.Rows(e.RowInd
imgStrAA = Me.C1Screen1.Rows(e.RowInd
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
If e.ColumnIndex = Me.C1Screen1.Columns("Imag
imgStr = Me.C1Screen1.Rows(e.RowInd
imgStrAA = Me.C1Screen1.Rows(e.RowInd
If ObjFrm Is Nothing Then ObjFrm = New FormImage(imgStr)
ObjFrm.ShowDialog()
ObjFrm = Nothing
End If
the problem is here ..
Me.C1Screen1.Columns(Me.C1 Screen1.Co lumns.Coun t - 1).Index
instead of Me.C1Screen1.Columns.Count - 1 use column name...
Me.C1Screen1.Columns(Me.C1
instead of Me.C1Screen1.Columns.Count
ASKER
I tried it but line
If e.ColumnIndex = Me.C1Screen1.Columns("Imag e") Then
returns error message:
Operator '=' is not defined for type integer and system.Widows.form.datagri dviewcolum n.
If e.ColumnIndex = Me.C1Screen1.Columns("Imag
returns error message:
Operator '=' is not defined for type integer and system.Widows.form.datagri
e.ColumnIndex = Me.C1Screen1.Columns("Imag e").index
ASKER
When i try If e.ColumnIndex = Me.C1Screen1.Columns("Imag eA").Index
no error but the link does not work.
no error but the link does not work.
Victor..count your indexes..try If e.ColumnIndex = Me.C1Screen1.Columns(10).I ndex
ASKER
Finally, I got it.
Thanks asgain.
V.
Thanks asgain.
V.
Take a look at DataGridViewLinkColumn Class
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewlinkcolumn(v=vs.90).aspx
Yiannis