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 DataGridLinkCol umn :
System.Windows. Forms.DataGridC olumnStyle
{
Hashtable links = new Hashtable();
public event LinkLabelLinkCl ickedEventHandl er LinkClicked;
public DataGridLinkCol umn()
{
}
void AddLinkControl( Rectangle r, int rowNum, object data, string text)
{
LinkLabel link = new LinkLabel();
link.Text = text;
link.Parent = this.DataGridTa bleStyle.DataGr id;
link.BackColor = ((rowNum & 1) == 1)?this.DataGri dTableStyle.Bac kColor
:this.DataGridT ableStyle.Alter natingBackColor ;
link.Links.Add( 0, text.Length, data);
link.Bounds = r;
link.LinkClicke d += new
LinkLabelLinkCl ickedEventHandl er(link_LinkCli cked);
links[rowNum] = link;
this.DataGridTa bleStyle.DataGr id.Controls.Add (link);
}
protected override void Abort(int rowNum)
{
}
protected override bool Commit(System.W indows.Forms.Cu rrencyManager
dataSource, int rowNum)
{
return false;
}
protected override void Edit(System.Win dows.Forms.Curr encyManager
source,
int rowNum, System.Drawing. Rectangle bounds, bool readOnly, string
instantText, bool cellIsVisible)
{
}
protected override void Edit(System.Win dows.Forms.Curr encyManager
source,
int rowNum, System.Drawing. Rectangle bounds, bool readOnly, string
instantText)
{
base.Edit (source, rowNum, bounds, readOnly, instantText);
}
protected override void Edit(System.Win dows.Forms.Curr encyManager
source,
int rowNum, System.Drawing. Rectangle bounds, bool readOnly)
{
base.Edit (source, rowNum, bounds, readOnly);
}
protected override int GetMinimumHeigh t()
{
return FontHeight+2;
}
protected override int GetPreferredHei ght(System.Draw ing.Graphics g,
object
value)
{
return GetMinimumHeigh t();
}
protected override System.Drawing. Size
GetPreferredSiz e(System.Drawin g.Graphics g, object value)
{
return new System.Drawing. Size(50, GetMinimumHeigh t());
}
protected override void Paint(System.Dr awing.Graphics g,
System.Drawing. Rectangle bounds, System.Windows. Forms.CurrencyM anager
source, int rowNum, bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.White,
Brushes.Blue,
alignToRight);
}
protected override void Paint(System.Dr awing.Graphics g,
System.Drawing. Rectangle bounds, System.Windows. Forms.CurrencyM anager
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 = GetColumnValueA tRow(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!=bo unds )
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(Dat aGrid value)
{
base.SetDataGri d (value);
value.Scroll += new EventHandler(Da taGrid_Scroll);
}
protected override void SetDataGridInCo lumn(DataGrid value)
{
base.SetDataGri dInColumn (value);
value.Scroll += new EventHandler(Da taGrid_Scroll);
}
private void DataGrid_Scroll (object sender, EventArgs e)
{
foreach( LinkLabel link in this.links.Valu es )
link.Visible = false;
}
private void link_LinkClicke d(object sender,
LinkLabelLinkCl ickedEventArgs
e)
{
if( LinkClicked!=nu ll )
LinkClicked( sender, e );
}
}
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 DataGridLinkCol umn :
System.Windows. Forms.DataGridC olumnStyle
{
Hashtable links = new Hashtable();
public event LinkLabelLinkCl ickedEventHandl er LinkClicked;
public DataGridLinkCol umn()
{
}
void AddLinkControl( Rectangle r, int rowNum, object data, string text)
{
LinkLabel link = new LinkLabel();
link.Text = text;
link.Parent = this.DataGridTa bleStyle.DataGr id;
link.BackColor = ((rowNum & 1) == 1)?this.DataGri dTableStyle.Bac kColor
:this.DataGridT ableStyle.Alter natingBackColor ;
link.Links.Add( 0, text.Length, data);
link.Bounds = r;
link.LinkClicke d += new
LinkLabelLinkCl ickedEventHandl er(link_LinkCli cked);
links[rowNum] = link;
this.DataGridTa bleStyle.DataGr id.Controls.Add (link);
}
protected override void Abort(int rowNum)
{
}
protected override bool Commit(System.W indows.Forms.Cu rrencyManager
dataSource, int rowNum)
{
return false;
}
protected override void Edit(System.Win dows.Forms.Curr encyManager
source,
int rowNum, System.Drawing. Rectangle bounds, bool readOnly, string
instantText, bool cellIsVisible)
{
}
protected override void Edit(System.Win dows.Forms.Curr encyManager
source,
int rowNum, System.Drawing. Rectangle bounds, bool readOnly, string
instantText)
{
base.Edit (source, rowNum, bounds, readOnly, instantText);
}
protected override void Edit(System.Win dows.Forms.Curr encyManager
source,
int rowNum, System.Drawing. Rectangle bounds, bool readOnly)
{
base.Edit (source, rowNum, bounds, readOnly);
}
protected override int GetMinimumHeigh t()
{
return FontHeight+2;
}
protected override int GetPreferredHei ght(System.Draw ing.Graphics g,
object
value)
{
return GetMinimumHeigh t();
}
protected override System.Drawing. Size
GetPreferredSiz e(System.Drawin g.Graphics g, object value)
{
return new System.Drawing. Size(50, GetMinimumHeigh t());
}
protected override void Paint(System.Dr awing.Graphics g,
System.Drawing. Rectangle bounds, System.Windows. Forms.CurrencyM anager
source, int rowNum, bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.White,
Brushes.Blue,
alignToRight);
}
protected override void Paint(System.Dr awing.Graphics g,
System.Drawing. Rectangle bounds, System.Windows. Forms.CurrencyM anager
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 = GetColumnValueA tRow(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!=bo unds )
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(Dat aGrid value)
{
base.SetDataGri d (value);
value.Scroll += new EventHandler(Da taGrid_Scroll);
}
protected override void SetDataGridInCo lumn(DataGrid value)
{
base.SetDataGri dInColumn (value);
value.Scroll += new EventHandler(Da taGrid_Scroll);
}
private void DataGrid_Scroll (object sender, EventArgs e)
{
foreach( LinkLabel link in this.links.Valu es )
link.Visible = false;
}
private void link_LinkClicke d(object sender,
LinkLabelLinkCl ickedEventArgs
e)
{
if( LinkClicked!=nu ll )
LinkClicked( sender, e );
}
}