tags:

views:

39

answers:

1

How can I add a hyperlink column for a Winforms DataGrid control?

Right now I am adding a string column like this

DataColumn dtCol = new DataColumn(); 
dtCol.DataType = System.Type.GetType("System.String");
dtCol.ColumnName = columnName;
dtCol.ReadOnly = true;
dtCol.Unique = false;
dataTable.Columns.Add(dtCol);

I just need it to be a hyperlink instead of a String. I am using C# with framework 3.5

+2  A: 

Use a DataGridViewLinkColumn.

The link shows an example of setting up the column and adding it to a DGV::

DataGridViewLinkColumn links = new DataGridViewLinkColumn();
links.UseColumnTextForLinkValue = true;
links.HeaderText = ColumnName.ReportsTo.ToString();
links.DataPropertyName = ColumnName.ReportsTo.ToString();
links.ActiveLinkColor = Color.White;
links.LinkBehavior = LinkBehavior.SystemDefault;
links.LinkColor = Color.Blue;
links.TrackVisitedState = true;
links.VisitedLinkColor = Color.YellowGreen;

DataGridView1.Columns.Add(links);

You'll probably be interested in this example that shows how the snippet above fits into a more complete example of configuring DGV columns at runtime.

Jay Riggs