0

I have a DataGridView in VB.Net (.NET Framework 4.0). I bind the data to it via "XXX.DataSource" a list of instances of my own class to this datagridview. My Class has public properties, which will automatically fill in the right cells of the row, because I am using the binding-mechanism.

Now, I have one cell per row (=one column), where I want to set the tooltip. The content of the tooltip-text is also in the instance of my class (e.g. additional property, function, etc...).

How can I use data of a binded object to set a tooltip in a datagridview-cell?

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Cit
  • 113
  • 1
  • 10

1 Answers1

0

You can use a DataGridView.CellFormatting Event to set the DataGridViewCell.ToolTipText Property. Their documentation provides an example on how to do this in C#, but in VB.NET it should look like:

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
    If (e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index) AndAlso e.Value IsNot Nothing Then
        Dim cell As DataGridViewCell = Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)

        If e.Value.Equals("*") Then
            cell.ToolTipText = "very bad"
        ElseIf e.Value.Equals("**") Then
            cell.ToolTipText = "bad"
        ElseIf e.Value.Equals("***") Then
            cell.ToolTipText = "good"
        ElseIf e.Value.Equals("****") Then
            cell.ToolTipText = "very good"
        End If
    End If
End Sub

What this is doing is getting the cell from the DataGridViewCellFormattingEventArgs event argument e and depending on its value, setting the tool tip to the specified text. You can set the cell tool tip text to any valid string that you would like, which if I am understanding your question correctly, you have a property in your class that contains the text for the tool tip.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • Thank you. But how can I reach an other property of the object-instance in this method?e.g. Property "Status" = cellvalue; Property "ErrorText" = Tooltip. – Cit Sep 01 '18 at 19:11
  • Something like here [link](https://stackoverflow.com/a/18700260) . But I do not know how two implement, because I get "many red lines"...in my VB.net-Code – Cit Sep 03 '18 at 05:42
  • All the stuff with "style" and "tooltipservice" is unknown. I think, the example is made on a WPF-control. – Cit Sep 04 '18 at 10:26
  • @Cit you did not say whether it was a WPF solution or web application. You may want to make use of a gridview instead if you are creating a web solution. Then, to get a tool tip, [try this](https://stackoverflow.com/questions/13298044/how-to-add-a-tooltip-on-mouse-over-on-the-grid-view-column-heading). It should be a matter of assigning the tool tip during the row databound event. You may also be able to do the same thing for a datagridview as well, but I know from experience that gridviews are easier to work with in ASP.NET, at least in my opinion. – Timothy G. Sep 04 '18 at 11:50
  • No, no, no! My app is a normal VB.Net-Application (no web, no WPF). Only the solution approach of my link (two comments before) seems to be running for WPF. It was more a suggestion. Is there an equal solution for windows-forms, maybe? – Cit Sep 04 '18 at 14:49