选择datagridview单元格时显示工具提示

Tro*_*hel 3 c# vb.net datagridview

如何在选择单元格时显示datagridview的工具提示,而不是从鼠标悬停但是使用箭头键?

Jay*_*ggs 7

正如您所注意到的,您将无法使用DataGridView的内置工具提示.实际上,您需要禁用它,因此将DataGridView的ShowCellToolTips属性设置为false(true默认情况下).

您可以使用CellEnter常规Winform ToolTip控件的DataGridView 事件来显示工具提示,因为无论是使用鼠标还是箭头键,焦点都会从一个单元格更改为单元格.

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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我根据单元格的高度和宽度在ToolTip的位置添加了一个偏移量.我做到了这一点,因此ToolTip不会直接出现在单元格上; 你可能想要调整这个设置.