问题
I have data bound DataGridView
in a desktop app with columns that have their ToolTipText
property set, yet no tool tip is displayed when I hover over grid view (cells or cell headers).
The ShowCellToolTips
property of the grid view is true
, and I have verified using break points that it is not changed programmatically before I mouse over.
I have tried creating a CellToolTipTextNeeded
event handler to see what the tool tip text was, but the event handler is never called.
Is there anything I have missed?
Thanks, Rob
Edit: We're using framework 2.0.
回答1:
It appears from your question that you set the tooltip text of the columns.
Columns tooltip text only appears when floating over the headers. To show tooltip text on the cells you have to hookup the CellToolTipTextNeeded
event and set the value of e.ToolTipText
in the event args
回答2:
Try using Cell.ToolTipText property. You will probably need to loop the rows of the DataGridView and set the tooltips manually:
For Each row As DataGridViewRow In Me.DataGridView.Rows
Me.DataGridView("MyCol", row.Index).ToolTipText = "MyToolTipText"
Next
Might not be suitable for a bound DataGridView with lots of rows but works successfully for me with an unbound DataGridView with a couple of hundred rows. Hope this helps.
回答3:
When I added a datagridview with a single (empty) column to a form, added text to the ToolTipText property for that column, and ensured that the ShowCellToolTips property for the datagridview is set to True, I do get a tooltip popup when I hover my mouse over that column's header. This seems to contradict what was stated in the original question, but in my test the grid was not data bound. Not sure if that makes a difference. However, on a project with a data bound datagridview, I just used a ToolTip component:
(1) Add a ToolTip component to your form.
(2) Set the ToolTip on toolTip1
(or equivalent name for your ToolTip component) property for your datagridview to whatever text you want to display.
(3) Set your datagridview's ShowCellToolTips property to False.
(4) Viola! Works as expected.
回答4:
I had a simular problem but was able to correct it by setting the ShowCellToolTip to true on my DataGridView. Once I did that I was able to send the following code and everything worked fine.
tableDocTypes.ShowCellToolTips = true;
tableDocTypes.Rows[i].Cells[columnFormCabinet.Index].ToolTipText = "Cabinet is not defined on the optical server.";
回答5:
To show the tooltip of the grid cell, you can use this event handler "CellToolTipTextNeeded". Refer the below code Snippet,
this.dataGridView1.ShowCellToolTips = true;
this.dataGridView1.CellToolTipTextNeeded += dataGridView1_CellToolTipTextNeeded;
void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
e.ToolTipText = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
}
回答6:
We ended up using a ToolTip widget and the CellMouseEnter
, CellMouseLeave
events to show it appropriately. Not optimal, but it works around the odd behaviour we were experiencing.
回答7:
I'm currently experiencing the same behvoiur on Framework 3.5. Is seems the DataSource property needs to be set in order to get the CelToolTipTextNeeded event to fire.
回答8:
Set datagridview ShowCellToolTips property to False
回答9:
I don't know if this tip is a solution to your specific problem, but do you use SP1 of VS2008 ? This Service Pack resolves many different issues, as I have discovered.
回答10:
I found this article looking for help on setting tooltips per row.
I just wanted to confirm that handling the CellToolTipText event works for me in VS2008 SP1.
For those of you who are wondering how the set the text to a value from the underlying datarow, this might be useful:
private void myDGV_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
// This is used to set tooltiptext for individual cells in the grid.
if (e.ColumnIndex == 2) // I only want tooltips for the second column (0-based)
{
if (e.RowIndex >= 0) // When grid is initialized rowindex == 0
{
// e.ToolTipText = "this is a test."; // static example.
DataRowView drv = ((DataGridView)sender).Rows[e.RowIndex].DataBoundItem as DataRowView;
MyTableRowClass theRow = drv.Row as MyTableRowClass;
e.ToolTipText = theRow.Col1 + "\r\n" + theRow.Col2;
}
}
}
回答11:
- set your DataGridView's
ShowCellToolTips
property tofalse
Put this code in your DataGridView's
CellMouseEnter
eventprivate void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e) { if (!(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewImageCell))) return; System.Windows.Forms.ToolTip tlp = new System.Windows.Forms.ToolTip(); tlp.SetToolTip(dgv, "Your ToolTipText"); }
回答12:
Had a related problem where CellToolTipTextNeeded would only occasionally be called. The behavior was tooltip over a tooltip when there was overflow in the cell. When WrapMode of the cell is set to true then the CellToolTipTextNeeded would be called correctly every time. I thought that the CellToolTipTextNeeded would be called and override the generic tooltip but it seems to call this event only on the first entry into the datagridview.cell and then, if mouse off the cell and back on (remaining in the datagridview), the "view overflow tooltip" shows instead...
Anyway info might help someone else out.
来源:https://stackoverflow.com/questions/389439/datagridview-tooltiptext-not-showing