-
Aug 28th, 2019, 02:48 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Help needed about strange behaviour of a datagridview and tooltip
While I was trying to get a tooltip to work on my DGV, I noticed that my DGV actually was already showing a pop-up when hovering over the header cells but only from 4th column and onward. I could not see anywhere in the code that I was doing it (I might have forgotten having done that already). I then deleted the tooltip control to check if it has any effect but the pup-up still shows up on header cells from the 4th column and on. The pop-up also contains the header cell text; very strange.
What could be doing that? And how can I find where in the code is this happening? I went through the whole events for the DGV but they are not responsible. I searched the entire project for events like "mouseOver" and "Hover" but no indication. Also searched for "> 3" to see if there is somewhere that I am doing this for those column indices; no luck.
Thanks for any tip
Last edited by Grand; Aug 28th, 2019 at 03:50 AM.
-
Aug 28th, 2019, 03:22 AM
#2
Addicted Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Dit you set the ToolTipText property for that column?
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Aug 28th, 2019, 03:39 AM
#3
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
I am not sure. Maybe I did at some time. How will I check that? and by the way, the project does not ahve a tooltip control. I have deleted it but still the tip shows up. or it might be somethings else?
-
Aug 28th, 2019, 03:41 AM
#4
Addicted Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Right click on the datagridview, Select edit columns.... double click on your column and look for the ToolTip property
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Aug 28th, 2019, 03:47 AM
#5
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
OK. I think I forgot to mention that my DGV is databound; sorry about that. This means there is no columns in design mode.
-
Aug 28th, 2019, 04:00 AM
#6
Addicted Member
Re: Help needed about strange behaviour of a datagridview and tooltip
in that case we would have to see your code, and proberbly your disigner form designer file to see whats going on.
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Aug 28th, 2019, 04:05 AM
#7
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
I can prepare that but you won't be able to run it as it interact with multiple tables on a SQL database which I cannot provide to you. Is there a way around this?
-
Aug 28th, 2019, 04:07 AM
#8
Addicted Member
Re: Help needed about strange behaviour of a datagridview and tooltip
The code is a good start to see whats going on. In the beginning i dont think we need the databse.
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Aug 28th, 2019, 04:35 AM
#9
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Originally Posted by Goggy
The code is a good start to see whats going on. In the beginning i dont think we need the databse.
thanks for helping out. Here is the code:Vbforums_2019-08-28.zip
-
Aug 28th, 2019, 04:12 AM
#10
Re: Help needed about strange behaviour of a datagridview and tooltip
not sure what your problem is, give this a try
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Rows.Add("1", "test1")
dt.Rows.Add("2", "test2")
dt.Rows.Add("3", "test3")
dt.Rows.Add("4", "test4")
DataGridView1.DataSource = dt
For Each r As DataGridViewRow In DataGridView1.Rows
If Not r.IsNewRow Then
For Each c As DataGridViewCell In r.Cells
If c.Value IsNot Nothing Then
c.ToolTipText = c.Value.ToString
End If
Next
End If
Next
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
DataGridView1.Columns(0).HeaderCell.ToolTipText = "Col1 Header ToolTip"
DataGridView1.Columns(1).HeaderCell.ToolTipText = "Col2 Header ToolTip"
If e.ColumnIndex = Me.DataGridView1.Columns(1).Index AndAlso (e.Value IsNot Nothing) Then
With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
If e.Value.Equals("test1") Then
.ToolTipText = "< Tooltip 1 >"
ElseIf e.Value.Equals("test2") Then
.ToolTipText = "< Tooltip 2 > "
ElseIf e.Value.Equals("test3") Then
.ToolTipText = "< Tooltip 3 >"
ElseIf e.Value.Equals("test4") Then
.ToolTipText = "< Tooltip 4 >"
End If
End With
End If
End Sub
hth
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Aug 28th, 2019, 04:39 AM
#11
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Originally Posted by ChrisE
not sure what your problem is, give this a try
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Rows.Add("1", "test1")
dt.Rows.Add("2", "test2")
dt.Rows.Add("3", "test3")
dt.Rows.Add("4", "test4")
DataGridView1.DataSource = dt
For Each r As DataGridViewRow In DataGridView1.Rows
If Not r.IsNewRow Then
For Each c As DataGridViewCell In r.Cells
If c.Value IsNot Nothing Then
c.ToolTipText = c.Value.ToString
End If
Next
End If
Next
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
DataGridView1.Columns(0).HeaderCell.ToolTipText = "Col1 Header ToolTip"
DataGridView1.Columns(1).HeaderCell.ToolTipText = "Col2 Header ToolTip"
If e.ColumnIndex = Me.DataGridView1.Columns(1).Index AndAlso (e.Value IsNot Nothing) Then
With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
If e.Value.Equals("test1") Then
.ToolTipText = "< Tooltip 1 >"
ElseIf e.Value.Equals("test2") Then
.ToolTipText = "< Tooltip 2 > "
ElseIf e.Value.Equals("test3") Then
.ToolTipText = "< Tooltip 3 >"
ElseIf e.Value.Equals("test4") Then
.ToolTipText = "< Tooltip 4 >"
End If
End With
End If
End Sub
hth
Thanks ChrisE. It worked on a fresh DGV but can't get it to work on my DGV. Also that I need to have a tooltip for header cells where I can get the content of the cell and then do something with it. I think this thread is to see if we can find out what my issue is. The other thread is more fitting to try a solution.
-
Aug 28th, 2019, 04:52 AM
#12
Re: Help needed about strange behaviour of a datagridview and tooltip
try this in the Form Load for HeaderCell
Code:
For Each co As DataGridViewColumn In DataGridView1.Columns
co.HeaderCell.ToolTipText = co.HeaderCell.Value
Next
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Aug 28th, 2019, 05:09 AM
#13
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Originally Posted by ChrisE
try this in the Form Load for HeaderCell
Code:
For Each co As DataGridViewColumn In DataGridView1.Columns
co.HeaderCell.ToolTipText = co.HeaderCell.Value
Next
It didn't work. I think, it is because that I am overwrting it somewhere else and I cannot figure it out where.
Also that I forgot to mention that the DGV content is dynamic/it changes based on the user selection of a combobox. So, we need to find a solution that looks at the current state of the DGV.
Did you have a look at my code?
-
Aug 28th, 2019, 05:30 AM
#14
Re: Help needed about strange behaviour of a datagridview and tooltip
no I didn't look at your code, I will if this doesn't work
this will read the HeaderCells to the Debug Window, see if they are correct
use a Button
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim headers = (From header As DataGridViewColumn In _
DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
Select header.HeaderText).ToArray
Debug.Print(String.Join(";", headers))
'or each Column in a new Line
'Debug.Print(String.Join(vbNewLine, headers))
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Aug 28th, 2019, 06:10 AM
#15
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Hi,
It gives me the text of each column header of my current DGV. e.g. :
Code:
JFIM;ModuleCode;CourseCode;DocTitle;JBLX
-
Aug 28th, 2019, 08:50 AM
#16
Re: Help needed about strange behaviour of a datagridview and tooltip
Originally Posted by Grand
I then deleted the tooltip control to check if it has any effect but the pup-up still shows up on header cells from the 4th column and on. The pop-up also contains the header cell text; very strange.
What could be doing that? And how can I find where in the code is this happening? I went through the whole events for the DGV but they are not responsible. I searched the entire project for events like "mouseOver" and "Hover" but no indication. Also searched for "> 3" to see if there is somewhere that I am doing this for those column indices; no luck.
That's the normal behaviour of the DGV Control when the width of the text in the cell is wider than the cell itself. In my experience, you also get this default pop-up behaviour when the text does actually fit the cell's width, but only just barely fits.
If you are happy to use the DGV's pop-up mechanism (as opposed to rolling your own with the ToolTip Class), and the DGV is bound, then you might want to look at the CellToolTipTextNeeded event.
Something like the following example should work for you:
vb.net Code:
Private Sub DataGridView1_CellToolTipTextNeeded(sender As Object, e As DataGridViewCellToolTipTextNeededEventArgs) Handles DataGridView1.CellToolTipTextNeeded
Dim dgv = DirectCast(sender, DataGridView)
If e.RowIndex = -1 AndAlso e.ColumnIndex <> -1 Then
e.ToolTipText = dgv.Columns(e.ColumnIndex).HeaderText & vbCrLf & "this is a column header"
Else
e.ToolTipText = "Not a column header cell"
End If
End Sub
-
Aug 30th, 2019, 03:19 AM
#17
Thread Starter
Fanatic Member
Re: Help needed about strange behaviour of a datagridview and tooltip
Originally Posted by Inferrd
That's the normal behaviour of the DGV Control when the width of the text in the cell is wider than the cell itself. In my experience, you also get this default pop-up behaviour when the text does actually fit the cell's width, but only just barely fits.
If you are happy to use the DGV's pop-up mechanism (as opposed to rolling your own with the ToolTip Class), and the DGV is bound, then you might want to look at the CellToolTipTextNeeded event.
Something like the following example should work for you:
vb.net Code:
Private Sub DataGridView1_CellToolTipTextNeeded(sender As Object, e As DataGridViewCellToolTipTextNeededEventArgs) Handles DataGridView1.CellToolTipTextNeeded Dim dgv = DirectCast(sender, DataGridView) If e.RowIndex = -1 AndAlso e.ColumnIndex <> -1 Then e.ToolTipText = dgv.Columns(e.ColumnIndex).HeaderText & vbCrLf & "this is a column header" Else e.ToolTipText = "Not a column header cell" End If End Sub
That is great. than you so much for clearing that up. Also your example solved my challenge as well.
Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|