关于如何向DataGridView的每一行前添加图标

Posted on 2015-03-27 17:36  攻城97  阅读(547)  评论(0)    收藏  举报

由于老板比较讲究,要求为DataGridView控件中的每一行前都添加一个小图标。

其实方法很简单,只需要在DataGridView的RowPostPaint事件中添加一行代码就可以了

1  private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
2         {
3             string strTip="报警";
4             Image image=WindowsFormsApplication18.Properties.Resources.Alert_1;
5             e.Graphics.DrawImage(image, e.RowBounds.Left + this.dataGridView1.RowHeadersWidth - 20, e.RowBounds.Top + 4, 16, 16);
6             this.dataGridView1.Rows[e.RowIndex].HeaderCell.ToolTipText = strTip;
7         }

为了防止他在找麻烦,我还赠送了他一个功能,就是当鼠标停靠在每条记录的最左边时,有一个小小的提示信息,告诉他这个图标的记录是关于什么的!效果如图:

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3