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

DataGrid: How to get hyperlinks?

24 views
Skip to first unread message

developers

unread,
Jul 24, 2003, 2:18:51 PM7/24/03
to
Hi All,

I have number of examples on customization of ColumnStyles, but I can't
figure how to get a column that would display a text as hyperlink, switch
mouse cursor to a pointy hand when hovering and fire an event if clicked?
OK the displaying part is easy, override the Paint, but the rest?
Just genereal digging direction would suffice...

TIA,
E.


developers

unread,
Jul 30, 2003, 1:47:42 PM7/30/03
to
Well,
<sarcasm>Thanks for overwelming response...</sarcasm>
Here's my implementation (read-only), which leaves a lot for improvement but
works:
---------------------- cut
here ----------------------------------------------
public sealed class DataGridLinkColumn :
System.Windows.Forms.DataGridColumnStyle
{
Hashtable links = new Hashtable();
public event LinkLabelLinkClickedEventHandler LinkClicked;
public DataGridLinkColumn()
{
}
void AddLinkControl(Rectangle r, int rowNum, object data, string text)
{
LinkLabel link = new LinkLabel();
link.Text = text;
link.Parent = this.DataGridTableStyle.DataGrid;
link.BackColor = ((rowNum & 1) == 1)?this.DataGridTableStyle.BackColor
:this.DataGridTableStyle.AlternatingBackColor;
link.Links.Add(0, text.Length, data);
link.Bounds = r;
link.LinkClicked += new LinkLabelLinkClickedEventHandler(link_LinkClicked);
links[rowNum] = link;
this.DataGridTableStyle.DataGrid.Controls.Add(link);
}
protected override void Abort(int rowNum)
{
}
protected override bool Commit(System.Windows.Forms.CurrencyManager
dataSource, int rowNum)
{
return false;
}
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string
instantText, bool cellIsVisible)
{
}
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string
instantText)
{
base.Edit (source, rowNum, bounds, readOnly, instantText);
}
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly)
{
base.Edit (source, rowNum, bounds, readOnly);
}
protected override int GetMinimumHeight()
{
return FontHeight+2;
}
protected override int GetPreferredHeight(System.Drawing.Graphics g, object
value)
{
return GetMinimumHeight();
}
protected override System.Drawing.Size
GetPreferredSize(System.Drawing.Graphics g, object value)
{
return new System.Drawing.Size(50, GetMinimumHeight());
}
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
source, int rowNum, bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.White,
Brushes.Blue,
alignToRight);
}
protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
source, int rowNum)
{
Paint( g, bounds, source, rowNum, false );
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
object o = GetColumnValueAtRow(source, rowNum);
string text = (o==null || o==DBNull.Value)?this.NullText:o.ToString();
if( links.Contains(rowNum) )
{
LinkLabel link = links[rowNum] as LinkLabel;
if( link.Bounds!=bounds )
link.Bounds = bounds;
link.Visible = true;
}
else
{
AddLinkControl( bounds, rowNum, source.List[rowNum], text );
}
}
public override bool ReadOnly
{
get
{
return true;
}
}
public string Format
{
get { return format; }
set { format = value; }
}
public IFormatProvider FormatInfo
{
get { return formatInfo; }
set { formatInfo = value; }
}
string format;
IFormatProvider formatInfo;
protected override void SetDataGrid(DataGrid value)
{
base.SetDataGrid (value);
value.Scroll += new EventHandler(DataGrid_Scroll);
}
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn (value);
value.Scroll += new EventHandler(DataGrid_Scroll);
}

private void DataGrid_Scroll(object sender, EventArgs e)
{
foreach( LinkLabel link in this.links.Values )
link.Visible = false;
}
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
e)
{
if( LinkClicked!=null )
LinkClicked( sender, e );
}
}
---------------------- cut
here ----------------------------------------------
It uses event bubbling, so you'll need to hookup only once in your form code
to handle the clicks.

Cheers,
E.

"developers" <nos...@spam.org> wrote in message
news:Old$i4gUDH...@tk2msftngp13.phx.gbl...

0 new messages