Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you add a hyperlink to a datagrid?

19 views
Skip to first unread message

Burak

unread,
Apr 7, 2004, 2:29:36 PM4/7/04
to
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

Travis Merkel

unread,
Apr 9, 2004, 8:06:06 PM4/9/04
to
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 MyColumnStyles
{
/// <summary>
/// Summary description for LinkLabelColumnStyle.
/// </summary>
public class LinkLabelColumnStyle : DataGridTextBoxColumn
{
#region Variables
private DataLinkLabel columnLinkLabel;
#endregion

#region Properties
public LinkLabel LinkLabel
{
get
{
return this.columnLinkLabel;
}
}
#endregion

#region Constructor
public LinkLabelColumnStyle()
{
this.columnLinkLabel = new DataLinkLabel();
this.columnLinkLabel.Click += new EventHandler(ColumnLinkLabel_Click);
}
#endregion

#region Edit
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();
}
#endregion

private void ColumnLinkLabel_Click(object sender, EventArgs e)
{
MessageBox.Show("WooHoo");
}
}

public class DataLinkLabel : LinkLabel
{
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.


Burak Gunay

unread,
Apr 12, 2004, 9:40:45 AM4/12/04
to

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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

0 new messages