当单元格被选中时,如何显示datagridview的工具提示?
发布于 2011-07-21 03:42:26
正如您已经注意到的,您将无法使用DataGridView的内置工具提示。实际上,您需要禁用它,以便将DataGridView的ShowCellToolTips属性设置为false (默认情况下为true )。
可以将DataGridView的CellEnter事件与常规的Winform ToolTip控件一起使用,以便在焦点从一个单元格更改到另一个单元格时显示工具提示,而不管这是使用鼠标还是箭头键完成的。
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) {
var cell = dataGridView1.CurrentCell;
var cellDisplayRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
toolTip1.Show(string.Format("this is cell {0},{1}", e.ColumnIndex, e.RowIndex),
dataGridView1,
cellDisplayRect.X + cell.Size.Width / 2,
cellDisplayRect.Y + cell.Size.Height / 2,
2000);
dataGridView1.ShowCellToolTips = false;
}请注意,我根据单元格的高度和宽度向ToolTip的位置添加了偏移量。我这样做是为了让ToolTip不会直接出现在单元格上;您可能需要调整此设置。
发布于 2014-01-25 03:27:37
Jay Riggs的答案就是我用的那个。此外,因为我需要更长的持续时间,所以我必须添加此事件,以便使工具提示消失。
private void dataGridView_MouseLeave(object sender, EventArgs e)
{
toolTip1.Hide(this);
}发布于 2011-07-21 03:20:45
dgv.CurrentCellChanged += new EventHandler(dgv_CurrentCellChanged);
}
void dgv_CurrentCellChanged(object sender, EventArgs e)
{
// Find cell and show tooltip.
}https://stackoverflow.com/questions/6767197
复制相似问题