Add Hyperlink Column for Winforms DataGrid Control

How to add a hyperlink column for a Winforms DataGrid control?

I am currently adding a row column similar to 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

+5
source share
1 answer

Use DataGridViewLinkColumn .

The link shows an example of setting up a column and adding it to 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 will probably be interested in this example , which shows how the above snippet fits into a more complete example of tuning DGV columns at runtime.

+5
source

All Articles