using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace WindowsApplication1
{
public class DataGridLinkColumn : System.Windows.Forms.DataGridTextBoxColumn
{
private DataGrid dataGrid;
private int currentRow = -1;
public delegate void LinkColumnClickedEventHandler(string hyperlink);
public event LinkColumnClickedEventHandler LinkColumnClicked;
public DataGridLinkColumn(DataGrid dataGrid)
{
this.dataGrid = dataGrid;
this.dataGrid.MouseMove += new MouseEventHandler(dataGrid_MouseMove);
this.dataGrid.MouseUp += new MouseEventHandler(dataGrid_MouseUp);
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
g.FillRectangle(backBrush, bounds);
string s = ((DataRowView)source.Current).DataView[rowNum].Row[this.MappingName].ToString();
Font f = this.DataGridTableStyle.DataGrid.Font;
Font font = new Font(f.FontFamily.Name, f.Size, (currentRow == rowNum ? FontStyle.Underline : FontStyle.Regular));
g.DrawString(s, font, Brushes.Blue, RectangleF.op_Implicit(bounds));
}
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
return;
}
private void dataGrid_MouseMove(object sender, MouseEventArgs e)
{
if (dataGrid != null)
{
currentRow = -1;
DataGrid.HitTestInfo hti = dataGrid.HitTest(e.X, e.Y);
if (hti.Type == DataGrid.HitTestType.Cell && this.MappingName == this.DataGridTableStyle.GridColumnStyles[hti.Column].MappingName) currentRow = hti.Row;
this.Invalidate();
}
}
private void dataGrid_MouseUp(object sender, MouseEventArgs e)
{
if (dataGrid != null)
{
DataGrid.HitTestInfo hti = dataGrid.HitTest(e.X, e.Y);
if (hti.Type == DataGrid.HitTestType.Cell && this.MappingName == this.DataGridTableStyle.GridColumnStyles[hti.Column].MappingName)
{
CurrencyManager cm = (CurrencyManager)dataGrid.BindingContext[dataGrid.DataSource, dataGrid.DataMember];
LinkColumnClicked(((DataRowView)cm.Current).DataView[hti.Row].Row[this.MappingName].ToString());
}
}
}
}
}