Hi,
I needed to build a data grid in which the first column has
hyperlinks rather than simple text. I found the following code on this
newsgroup and it works fine except for one problem. When I click the
column headers all the columns sort except the hyperlink one. I
checked the underlying dataview and that does get sorted so it seems
like it is more of a display issue. I am guessing it has something to
do with the hashtable used by the code below but I am not quite sure
how to handle it. Any help will be appreciated.
Thanks,
Rudolph
CODE:
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 );
}
}