Click here to Skip to main content
Licence CDDL
First Posted 20 Oct 2005
Views 61,724
Bookmarked 33 times

LinkLabel DataGrid Column

By | 20 Oct 2005 | Article
A simple DataGrid column for LinkLabels.
 
Part of The SQL Zone sponsored by
See Also

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

    'Other columns...

    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)
    'Click handler code goes here.
    'if you need to access data of the row clicked
    '(for example to open a browser with the clicked url)
    'DirectCast(e.Link.LinkData, DataRowView)
    'contains the whole clicked row.
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.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

Lino Barreca

Software Developer (Senior)

Italy Italy

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalproblem Pinmemberjose angelo santos1:15 14 Jan '09  
Generalthanks Pinmemberbrigno8:47 12 Jan '07  
Questionhelp Pls Pinmemberlionvnit22:34 29 Oct '06  
AnswerRe: help Pls PinmemberLino Barreca23:40 29 Oct '06  
GeneralRe: help Pls Pinmemberlionvnit0:12 30 Oct '06  
QuestionUnnecessarily Complicated? PinmemberJay M Miller16:16 12 Apr '06  
AnswerRe: Unnecessarily Complicated? PinmemberLino Barreca21:40 12 Apr '06  
AnswerRe: Unnecessarily Complicated? Pinmemberimadulhaq7:53 25 May '06  
GeneralRe: Unnecessarily Complicated? Pinmemberimadulhaq7:54 25 May '06  
QuestionHow to incorporate your file into a project Pinmembersoloflexx8:10 19 Jan '06  
GeneralClick Event Handler Pinmemberjunoface4:45 13 Dec '05  
GeneralRe: Click Event Handler PinmemberLino Barreca22:01 13 Dec '05  
GeneralRe: Click Event Handler Pinmemberjunoface1:17 14 Dec '05  
GeneralVery very nice, but some problems PinmemberDesma21:52 30 Nov '05  
GeneralRe: Very very nice, but some problems PinmemberLino Barreca22:18 30 Nov '05  
GeneralVery useful!! PinmemberPazzoDiTe12:41 21 Oct '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 Oct 2005
Article Copyright 2005 by Lino Barreca
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid