winform dataGridview 为每一个单元格制定一个tooptip
1.为每一个单元格制定一个tooptip提示,当鼠标移到单元格时,自动弹出提示框。
界面如下:
1,代码如下,
先设置鼠标移到单元格的行号和列号:
View Code 1 public partial class DataGridViewMerge : Form
2 {
3 private int CellColumnIndex { get; set; }
4 private int CellRowIndex { get; set; }
2 {
3 private int CellColumnIndex { get; set; }
4 private int CellRowIndex { get; set; }
设置鼠标进入单元格显示tooltip工具窗口。
如下:
View Code 1 private void dgv1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
2 {
3 if (e.RowIndex < 0 || e.ColumnIndex < 0)
4 {
5 return;
6 }
7
8 this.toolTip1.Hide(this.dgv1);
9
10 this.CellColumnIndex = e.ColumnIndex;
11 this.CellRowIndex = e.RowIndex;
12 if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
13 {
14 int dgvY = dgv1.Location.Y;
15 int dgvX = dgv1.Location.X;
16 int cellX = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).X;
17 int cellY = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).Y;
18 int x = dgvX + cellX;
19 int y = dgvY + cellY+5;
20 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
21 // Point mousePos = PointToClient(MousePosition);
22 Point mousePos = new Point(x, y);
23 Rectangle rec = dgvc.ContentBounds;
24 string tip = "Tip is " + dgvc.Value.ToString()+" "+x+" "+y;
25
26 this.toolTip1.Show(tip, this.dgv1, mousePos);
27
28
29 }
30
31 }
2 {
3 if (e.RowIndex < 0 || e.ColumnIndex < 0)
4 {
5 return;
6 }
7
8 this.toolTip1.Hide(this.dgv1);
9
10 this.CellColumnIndex = e.ColumnIndex;
11 this.CellRowIndex = e.RowIndex;
12 if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
13 {
14 int dgvY = dgv1.Location.Y;
15 int dgvX = dgv1.Location.X;
16 int cellX = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).X;
17 int cellY = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).Y;
18 int x = dgvX + cellX;
19 int y = dgvY + cellY+5;
20 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
21 // Point mousePos = PointToClient(MousePosition);
22 Point mousePos = new Point(x, y);
23 Rectangle rec = dgvc.ContentBounds;
24 string tip = "Tip is " + dgvc.Value.ToString()+" "+x+" "+y;
25
26 this.toolTip1.Show(tip, this.dgv1, mousePos);
27
28
29 }
30
31 }
鼠标离开单元格时,隐藏tooltip
代码如下:
CellMouseLeave private void dgv1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
this.toolTip1.Hide(this.dgv1);
}
{
this.toolTip1.Hide(this.dgv1);
}
参考:tooltip汇总

浙公网安备 33010602011771号