How do you add a hyperlink to a datagrid?

B

Burak

Hello,

I have a windows forms datagrid,

Dim da As New OleDbDataAdapter(sql, conn)
da.Fill(ds, "providers")

' set the column widths
Dim c0 As New DataGridTextBoxColumn
'c0.TextBox.Enabled = False
c0.MappingName = "PROV_ID"
c0.HeaderText = "ID"
c0.Width = 45

' clear out the table style
dtgResults.TableStyles.Clear()

' create new table style
Dim style As New DataGridTableStyle
' set mapping name to table name
style.MappingName = "providers"
' add column widths to the style
style.GridColumnStyles.Add(c0)
' add style to the data grid
dtgResults.TableStyles.Add(style)

'bind the data source
dtgResults.DataSource = ds.Tables(0)
dtgResults.Refresh()

I want to make PROV_ID clickable, how can I do this?

Thank you,

Burak
 
G

Guest

You have to create a custom column style to do this. Currently you're only provided a textbox and a boolean column style. Here is code for a column style to do what you want. However it's in C# so you'll have to translate.

using System
using System.Data
using System.Drawing
using System.Windows.Forms

namespace MyColumnStyle

/// <summary
/// Summary description for LinkLabelColumnStyle
/// </summary
public class LinkLabelColumnStyle : DataGridTextBoxColum

#region Variable
private DataLinkLabel columnLinkLabel
#endregio

#region Propertie
public LinkLabel LinkLabe

ge

return this.columnLinkLabel


#endregio

#region Constructo
public LinkLabelColumnStyle(

this.columnLinkLabel = new DataLinkLabel()
this.columnLinkLabel.Click += new EventHandler(ColumnLinkLabel_Click)

#endregio

#region Edi
protected override void Edit(CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible

base.Edit(source,rowNum,bounds,readOnly,instantText,cellIsVisible)

this.columnLinkLabel.Parent = this.TextBox.Parent
this.columnLinkLabel.Location = this.TextBox.Location
this.columnLinkLabel.Size = new Size(this.TextBox.Width,this.TextBox.Height)
this.TextBox.Visible = false

this.columnLinkLabel.Text = this.TextBox.Text
this.columnLinkLabel.Visible = true
this.columnLinkLabel.BringToFront()
this.columnLinkLabel.Focus()

#endregio

private void ColumnLinkLabel_Click(object sender, EventArgs e

MessageBox.Show("WooHoo")



public class DataLinkLabel : LinkLabe

private int WM_KEYUP = 257

protected override void WndProc(ref Message m

if (m.Msg == WM_KEYUP

return


base.WndProc (ref m)




In the link label click event handler is where you'll want to put you action code

Hope this helps
 
B

Burak Gunay

Thank you Travis for the code.

I had a quick question: is it possible to compile this C# class and
call it from a vb.net project?

Thank you,

Burak
 
G

Guest

Burak,

I don't see why not. That's part of the beauty of .Net. Compile away, reference the assembly, and use the class. Should work beautifully.
 
B

Burak Gunay

Travis,

I used the foillowing page to translate the C# code to vb.net.
http://authors.aspalliance.com/aldotnet/examples/translate.aspx

Namespace MyColumnStyles

Public Class LinkLabelColumnStyle

Inherits DataGridTextBoxColumn

Public columnLinkLabel As DataLinkLabel

Public ReadOnly Property LinkLabel() As LinkLabel

Get
Return Me.columnLinkLabel
End Get
End Property

Public Sub New()
Me.columnLinkLabel = New DataLinkLabel
AddHandler Me.columnLinkLabel.Click, AddressOf
ColumnLinkLabel_Click
End Sub 'New

Private Sub ColumnLinkLabel_Click(ByVal sender As Object, ByVal
e As EventArgs)
'MsgBox(Me.columnLinkLabel.Text)
global_vars.PROVIDS = Me.columnLinkLabel.Text
'SearchProvider.ActiveForm.Controls.
'SearchProvider.ActiveForm.Close()
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As
CurrencyManager, ByVal rowNum As Integer, ByVal bounds As
System.Drawing.Rectangle, ByVal read As Boolean, ByVal instantText As
String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, read, instantText,
cellIsVisible)

Me.columnLinkLabel.Parent = Me.TextBox.Parent
Me.columnLinkLabel.Location = Me.TextBox.Location
Me.columnLinkLabel.Size = New Size(Me.TextBox.Width,
Me.TextBox.Height)
Me.TextBox.Visible = False

Me.columnLinkLabel.Text = Me.TextBox.Text
Me.columnLinkLabel.Visible = True
Me.columnLinkLabel.BringToFront()
Me.columnLinkLabel.Focus()

End Sub 'Edit
Public Class DataLinkLabel
Inherits LinkLabel
Private WM_KEYUP As Integer = 257


Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_KEYUP Then
Return
End If

MyBase.WndProc(m)
End Sub 'WndProc
End Class 'DataLinkLabel

End Class
End Namespace

It was working fine, but for some reason it stated giving errors on
MyBase.WndProc(m) lately.

Is there a way to modify the code so that the links will appear in the
grid right from the start?

I tried the folowing code:

Dim c0 As New MyColumnStyles.LinkLabelColumnStyle
c0.MappingName = "PROV_ID"
c0.HeaderText = "ID"
c0.Width = 45

Dim style As New DataGridTableStyle
style.MappingName = "providers"
style.GridColumnStyles.Add(c0)

dtgResults.TableStyles.Add(style)
dtgResults.DataSource = ds.Tables(0)

Dim i As Integer
Dim x As New MyColumnStyles.LinkLabelColumnStyle
For i = 0 To ds.Tables(0).Rows.Count - 1
x = CType(dtgResults.Item(i, 0), MyColumnStyles.LinkLabelColumnStyle)
x.columnLinkLabel.Parent = x.TextBox.Parent
x.columnLinkLabel.Location = x.TextBox.Location
x.columnLinkLabel.Size = New Size(x.TextBox.Width, x.TextBox.Height)
x.TextBox.Visible = False

x.columnLinkLabel.Text = x.TextBox.Text
x.columnLinkLabel.Visible = True
x.columnLinkLabel.BringToFront()
x.columnLinkLabel.Focus()
Next

but I get a "Specified cast is not valid" error on line

x = CType(dtgResults.Item(i, 0), MyColumnStyles.LinkLabelColumnStyle)

Is there a way to make the links appear without having to edit the
cell?

Thank you,

Burak
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top