Winform dataGridview 为每一个单元格制定一个tooptip

1.为每一个单元格制定一个tooptip提示,当鼠标移到单元格时,自动弹出提示框。

界面如下:

 

 

1,代码如下,

   先设置鼠标移到单元格的行号和列号:

public partial class DataGridViewMerge : Form
{
         private int CellColumnIndex { get; set; }
        private int CellRowIndex { get; set; }
}

  

设置鼠标进入单元格显示tooltip工具窗口。

如下:

private void dgv1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
         {
             if (e.RowIndex < 0 || e.ColumnIndex < 0)
             {
                 return;
             }
            
             this.toolTip1.Hide(this.dgv1);
 
             this.CellColumnIndex = e.ColumnIndex;
             this.CellRowIndex = e.RowIndex;
             if (this.CellColumnIndex >= 0 && this.CellRowIndex >= 0)
             {
               int dgvY = dgv1.Location.Y;
                 int dgvX = dgv1.Location.X;
                 int cellX = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).X;
                 int cellY = dgv1.GetCellDisplayRectangle(CellColumnIndex,CellRowIndex,false).Y;
                 int x = dgvX + cellX;
                 int y = dgvY + cellY+5; 
                 DataGridViewCell dgvc = this.dgv1[this.CellColumnIndex, this.CellRowIndex];
                // Point mousePos = PointToClient(MousePosition);
                 Point mousePos = new Point(x, y);
                  Rectangle rec = dgvc.ContentBounds;
                 string tip = "Tip is " + dgvc.Value.ToString()+" "+x+" "+y;
  
                 this.toolTip1.Show(tip, this.dgv1, mousePos);
                 
 
             }
              
         }

  

 鼠标离开单元格时,隐藏tooltip

代码如下:

private void dgv1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
        this.toolTip1.Hide(this.dgv1);
}

  

posted @ 2012-09-19 17:31  沙耶  阅读(440)  评论(0编辑  收藏  举报