博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGridView的CellFormatting事件和CellPainting事件
阅读量:7079 次
发布时间:2019-06-28

本文共 4904 字,大约阅读时间需要 16 分钟。

1
CellFormatting
事件,一般重绘单元格属性。

    private Bitmap highPriImage;
    private Bitmap mediumPriImage;
    private Bitmap lowPriImage;
private void dataGridView1_CellFormatting(object sender, 
        System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {
        // Set the background to red for negative values in the Balance column.
        if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
        {
            Int32 intValue;
            if (Int32.TryParse((String)e.Value, out intValue) && 
                (intValue < 0))
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.SelectionBackColor = Color.DarkRed;
            }
        }
        // Replace string values in the Priority column with images.
        if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
        {
            // Ensure that the value is a string.
            String stringValue = e.Value as string;
            if (stringValue == null) return;
            // Set the cell ToolTip to the text value.
            DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
            cell.ToolTipText = stringValue;
            // Replace the string value with the image value.
            switch (stringValue)
            {
                case "high":
                    e.Value = highPriImage;
                    break;
                case "medium":
                    e.Value = mediumPriImage;
                    break;
                case "low":
                    e.Value = lowPriImage;
                    break;
            }
        }
    }
2
CellPainting
事件,一般用于合并单元格用

Windows Forms DataGridView 
没有提供合并单元格的功能,要实现合并单元格的功能就要在
CellPainting
事件中使用
Graphics.DrawLine
 Graphics.DrawString 
自己来

下面的代码可以对
DataGridView
1
列内容相同的单元格进行合并:

 #region"
合并单元格的测试
"
private int? nextrow = null;
private int? nextcol = null;
private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        if (this.nextcol != null & e.ColumnIndex == this.nextcol)
        {
            e.CellStyle.BackColor = Color.LightBlue;
            this.nextcol = null;
        }
        if (this.nextrow != null & e.RowIndex == nextrow)
        {
            e.CellStyle.BackColor = Color.LightPink;
            this.nextrow = null;
        }
        if (e.RowIndex != this.dataGridView1.RowCount - 1)
        {
            if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
            {
                e.CellStyle.BackColor = Color.LightPink;
                nextrow = e.RowIndex + 1;
            }
        }
    }
    if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {

        if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
        {

            e.CellStyle.BackColor = Color.LightBlue;
            nextcol = e.ColumnIndex + 1;
        }
    }
}
//==========================
       
//
绘制单元格

private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
 
    //
纵向合并

    if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {

            using (Pen gridLinePen = new Pen(gridBrush))
            {

                // 
擦除原单元格背景

                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
               
绘制线条
,
这些线条是单元格相互间隔的区分线条
,
               
因为我们只对列
name
做处理
,
所以
datagridview
自己会处理左侧和上边缘的线条

                if (e.RowIndex != this.dataGridView1.RowCount - 1)
                {
                    if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                    {
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//
下边缘的线

                        //
绘制值

                        if (e.Value != null)
                        {
                            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                Brushes.Crimson, e.CellBounds.X + 2,
                                e.CellBounds.Y + 2, StringFormat.GenericDefault);
                        }
                    }
                }
                else
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//
下边缘的线

                    //
绘制值

                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }
                //
右侧的线

                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);
                e.Handled = true;
            }
        }
    }
    //
横向合并

    if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {

            using (Pen gridLinePen = new Pen(gridBrush))
            {

                // 
擦除原单元格背景

                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                {
                    //
右侧的线

                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    //
绘制值

                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }
                //
下边缘的线

                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                e.Handled = true;
            }
        }
    }
}
#endregion
本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/201801,如需转载请自行联系原作者
你可能感兴趣的文章
交换机802.1X认证配置
查看>>
苹果谷歌诺基亚50年后的LOGO会变成啥样?
查看>>
烂在肚子里的救命知识!看看吧!
查看>>
week 3
查看>>
导出的Java程序无法连接数据库的问题解决方案
查看>>
Windows Linux系统U盘制作工具
查看>>
oracle体系结构之 DB高速缓存池
查看>>
我的友情链接
查看>>
华为Agile Controller-Campus web界面admin管理员密码不对无法登陆
查看>>
遇到多个构造器参数时要考虑用构建器
查看>>
windos 8 虚拟光驱/硬盘技术
查看>>
WLC和汇聚交换机的配置
查看>>
使用python-gitlab的API V4来批量创建projects
查看>>
CLR.Via第三版第二章 生成、打包、部署和管理i应用程序及类型(
查看>>
Play Framework 2.3.x开始
查看>>
rz sz上传下载工具使用指南
查看>>
Spring与Ehcache简单自定义监听器配置
查看>>
关于ha高可用性的安装,ClusterIP和tomcat的配置
查看>>
我的系统我做主-----深度裁剪红帽5.8系统过程演示(只有5M哦)
查看>>
nosql的使用
查看>>