Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding A Hyperlink Column To A DataGrid In Winform

Status
Not open for further replies.

dtqbterror

Programmer
May 17, 2001
87
US
I have been searching all over the internet for an example of how to add a new column style to the winform datagrid so that the data contained will be a hyperlink. I have been unable to find a good example of this. Does anyone have a link to a tutorial that discusses this.

Thanks.

 
I've written the following custom grid column. It's basic, but you can always extend it to include a LinkBehavior property and maybe a hand cursor on hover.

Hope it helps.

The column style:
Code:
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());
				}
			}
		}
	}
}

And a test form:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			DataTable dt = new DataTable("Test");

			dt.Columns.Add(new DataColumn("Link", typeof(string)));

			for (int i = 0; i < 5; i++)
			{
				DataRow dr = dt.NewRow();

				dr[0] = "[URL unfurl="true"]www.microsoft.com/test"[/URL] + i.ToString();

				dt.Rows.Add(dr);
			}

			DataGridTableStyle ts = new DataGridTableStyle();

			DataGridLinkColumn lc = new DataGridLinkColumn(dataGrid1);

			lc.LinkColumnClicked += new DataGridLinkColumn.LinkColumnClickedEventHandler(lc_LinkColumnClicked);

			lc.MappingName = "Link";

			lc.Width = 200;

			ts.GridColumnStyles.Add(lc);

			ts.MappingName = "Test";

			dataGrid1.TableStyles.Add(ts);

			dataGrid1.DataSource = dt;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(32, 24);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(384, 200);
			this.dataGrid1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(464, 266);
			this.Controls.Add(this.dataGrid1);
			this.Name = "Form1";
			this.Text = "Form1";
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		public static void Main(string[] args)
		{
			Application.Run(new Form1());
		}

		private void lc_LinkColumnClicked(string hyperlink)
		{
			MessageBox.Show(hyperlink);
		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top