Introduction
I needed to include a DataGrid column with a click-able cell. I found no valid solutions on the net (except some sold control packages and some tricks like mousedown and mousemove/hit-test on the grid) and I saw that many were asking for it... I decided to write it on my own. It's very simple and basic but it works very well.
This is what I needed and this is what I wrote, you may want to adapt it to your needs.
Missing you-may-need-them things (add them yourself)
- Font change on the grid.
- Right to left implementation.
- Visited links.
- Underline with no mouse on it (I needed the underline to appear when the mouse moves over the link text).
- Custom colorization for the link.
How it looks
| Normal grid |
Cursor in cell |
Cursor on link |
Cursor on link (row selected) |
|

|

|

|

|
How it works
The control overrides some DataGridColumnStyle methods (as you have to do when you write a custom ColumnStyle). When the control is initialized (in its constructor) a generic LinkLabel (this will be the main component that reacts to the clicks) is initialized with its standard size. When the TableStyle is bound to a DataGrid the control binds to some events.
MouseMove - Used to show/hide the control when the user moves the cursor on the grid.
DataSourceChanged - Used to hide the control (if visible) when the Datagrid.Datasource changes.
BindingContextChanged - Used to hide the control (if visible) when the grid binding to data source changes.
PositionChanged (DataBinding) - Used to hide the control (if visible) when the position of the binding in the datasource changes. (IE: you click the link and the DataGrid.Datasource is loaded with different data).
Overridden methods
- Editing:
Abort, Commit, Edit
- Painting:
Paint (with overloads)
- Cell size:
GetMinimumHeight, GetPreferredHeight, GetPreferredSize
- Handling:
SetDataGridInColumn, ConcedeFocus
How to use it
(Code used to take the pictures above)
Private Sub SetGridStyle()
Dim grdStyle As New DataGridTableStyle
Dim grdStileColEqv As New DataGridColumns.LinkLabel
grdStileColEqv.MappingName = "Equivalenti"
grdStileColEqv.HeaderText = "Eqv."
grdStileColEqv.Alignment = HorizontalAlignment.Center
grdStileColEqv.NullText = ""
grdStileColEqv.Width = 40
AddHandler grdStileColEqv.LinkClicked, AddressOf LinkClicked
grdStyle.GridColumnStyles.Add(grdStileColEqv)
grdLista.TableStyles.Add(grdListaStyle)
End Sub
Private Sub LinkClicked(ByVal sender As Object, _
ByVal e As LinkLabelLinkClickedEventArgs)
End Sub
Comments
I hope you'll find this code useful. If you want to contribute fixing bugs and/or adding functions, let me know. I'll modify the code accordingly.