Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a hyperlink column for a Winforms DataGrid control

Tags:

winforms

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

like image 850
patrick Avatar asked Sep 21 '10 21:09

patrick


1 Answers

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.

like image 118
Jay Riggs Avatar answered Sep 27 '22 00:09

Jay Riggs