.NET WinForm中的DataGridView为程序开发提供了诸多的便利,我们不需要做许多额外的工作就可以获得一些基础功能,例如点击列标题排序、行选择功能、改变列宽和行宽,以及单元格内容的自动ToolTip功能等。但DataGridView给我们带来这些开发便利的同时也制造了一些小麻烦,例如对于单元格的自动ToolTip功能,我们如何才能自定义ToolTip提示框的样式呢?这里有一段代码可以帮助我们完成这个功能。

  首先你需要在窗体上添加一个ToolTip控件,然后参考下面的代码给你的DataGridView添加CellMouseMove和MouseLeave事件,同时还需要给ToolTip控件添加Draw事件以定义ToolTip的具体呈现。

public partial class MainForm : Form
{
    private int cellColumnIndex = -1, cellRowIndex = -1;

public MainForm()
    {
        InitializeComponent();

this.testDataGridView.ShowCellToolTips = false;
        this.toolTip.AutomaticDelay = 0;
        this.toolTip.OwnerDraw = true;
        this.toolTip.ShowAlways = true;
        this.toolTip.ToolTipTitle = "Custom Tooltip: ";
        this.toolTip.UseAnimation = false;
        this.toolTip.UseFading = false;
    }

private void testDataGridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
        {
            return;
        }

this.toolTip.Hide(this.testDataGridView);
        this.cellColumnIndex = e.ColumnIndex;
        this.cellRowIndex = e.RowIndex;

if (this.cellColumnIndex >= 0 && this.cellRowIndex >= 0)
        {
            Point mousePos = PointToClient(MousePosition);
            string tip = "Tip is " + this.testDataGridView[this.cellColumnIndex, this.cellRowIndex].Value.ToString();
            this.toolTip.Show(tip, this.testDataGridView, mousePos);
        }
    }

private void testDataGridView_MouseLeave(object sender, EventArgs e)
    {
        this.toolTip.Hide(this.testDataGridView);
    }

private void toolTip_Draw(object sender, DrawToolTipEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);
        e.Graphics.DrawRectangle(Pens.Chocolate, new Rectangle(0, 0, e.Bounds.Width - 1, e.Bounds.Height - 1));
        e.Graphics.DrawString(this.toolTip.ToolTipTitle + e.ToolTipText, e.Font, Brushes.Red, e.Bounds);
    }
}

  testDataGridView是DataGridView的ID,toolTip是ToolTip的ID。

.NET WinForm中给DataGridView自定义ToolTip并设置ToolTip的样式相关推荐

  1. [C#] winform中的DataGridView的列宽设置(自动调整列宽)

    找了很多都说DataGridView有一个属性AutoSizeColumnMode,他有很多枚举值: 1.AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格.  2.AllCe ...

  2. datagridview固定列宽_[C#] winform中的DataGridView的列宽设置(自动调整列宽)

    1.AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格. 2.AllCellsExceptHeader 调整列宽,以适合该列中的所有单元格的内容,不包括标题单元格. 3.Col ...

  3. winform中的DataGridView的列宽设置(自动调整列宽)

    找了很多都说DataGridView有一个属性AutoSizeColumnMode,他有很多枚举值: 1.AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格.   2.AllC ...

  4. datagridview固定列宽_winform中的DataGridView的列宽设置

    DataGridView有一个属性AutoSizeColumnMode,他有很多枚举值: 1.AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格. 2.AllCellsExce ...

  5. WinForm中关于DataGridView控件的一些应用

    转载于新浪 美林居士 的博客:            blog.sina.com.cn/s/blog_797a56d20101daiw.html4 在.NET4.0中,以表格形式存储的数据通常是Dat ...

  6. Winform中给DataGridView添加多选框列并获取选中行的内容

    场景 使用NPOI导入Excel并赋值给DataTable,然后显示在DataGrdView上,并且添加多选框,然后获取选中行的内容. Winform中使用NPOI实现Excel导入并赋值给DataT ...

  7. WinForm中给DataGridView添加 自动编号

    //实现功能 DataGridView 添加 自动编号 DataTable table = DataTable(); DataColumn column = DataColumn(); Column. ...

  8. winform 中 给DataGridView的表头添加CheckBox

    在C/S架构中,给DataGridView的表头添加CheckBox控件: 添加类: /// <summary> /// 给DataGridView添加全选 /// </summar ...

  9. Winform中打印 dataGridView里的内容

    最近评论问题比较多,这是几年前得代码了,今天正好有时间我重新整理了下代码把源码Demo发上来给大家看看互相学习. 有问题随时交流.没有积分得私信我发你. Demo地址:DataGirdView打印.r ...

最新文章

  1. 数据库抽取,生成CSV文件导出,CSVUtils工具类
  2. 美多商城之验证码(异步方案)
  3. 朴素贝叶斯--文档分类
  4. ASP.NET Core MVC 之依赖注入 Controller
  5. java学习(174):constructor类反射编程
  6. struts2 html post large data,struts2文件上传限制大小问题
  7. 单片机跑马灯源代码+仿真
  8. STM32F407 USB CDC调试与经验总结
  9. 语法分析器实现过程(java)
  10. QQ小游戏 微信小游戏 即时通信 IM 删除会话 deleteConversation sdk
  11. Pr 入门教程:如何向影片中的剪辑添加过渡效果?
  12. 个人网站Timonj(Personal website)
  13. 服务器 异常自动关机,服务器自动关机常见问题
  14. 分享43个职场必备excel函数!(内含详细案例解析及教程参考)
  15. java代码是干嘛的_程序员究竟是干嘛的呢?
  16. 1. stm32h7 移植RTX5(AC6) 基于CUBEMX
  17. 思维导图 基础篇(11)应用-文章分析-框架法
  18. 注意这是ACfly TI芯片版本的程序
  19. C程序设计语言(第2版)
  20. 用vue写一个npm包(package),发布及引用

热门文章

  1. 雷林鹏分享:Lua 错误处理
  2. 自学编程的朋友,我想给你们这 5 个建议
  3. CF1146H Satanic Panic
  4. python 函数(二)
  5. MySQL数据库之安装
  6. Centos6下安装中文字体
  7. 单元测试(VS CppUnitTest)
  8. ElasticSearch elasticsearch-servicewrapper 在linux上的安装部署全程记录
  9. CAST 和 CONVERT的用法和区别
  10. ssh连接卡在【To escape to local shell, press ‘Ctrl+Alt+]‘.】的解决方法