我的博客

posts(42) comments(30) trackbacks(5)
  • 博客园
  • 联系
  • 订阅 订阅
  • 管理

News

昵称:Jackey
园龄:5年
粉丝:2
关注:0

搜索

 

常用链接

随笔分类

  • Ajax编程(3)
  • C# Winform(5)
  • Javascript(11)
  • Office操作(2)
  • SQL Server(6)
  • vs2005 C# .net(5)
  • 通用函数(11)

随笔档案

  • 2008年2月 (1)
  • 2007年11月 (1)
  • 2007年10月 (9)
  • 2007年9月 (6)
  • 2007年7月 (1)
  • 2007年6月 (1)
  • 2007年5月 (13)
  • 2007年4月 (4)
  • 2007年1月 (6)

文章分类

  • 成功学(1)
  • 基金由浅到深(8)
  • 我写的文章(2)
  • 心情日记(4)

相册

  • 我的相册

最新评论

阅读排行榜

评论排行榜

推荐排行榜

View Post

DataGridView重绘代码参考--C#

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

 

绿色通道:好文要顶关注我收藏该文与我联系

posted on 2007-10-05 03:46 Jackey 阅读(2645) 评论(0) 编辑 收藏

注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· 猎聘网网络招聘新势力
· 京东被指压榨供货商续:苛刻条款或只针对小企业
· Facebook公布犯罪团伙名单被批妨碍司法调查
· 传谷歌正开发家庭娱乐系统 以自有品牌销售
· 平安信托折戟Facebook 中国富豪梦碎IPO盛宴
» 更多新闻...
最新知识库文章:
· 如何学习一门新的编程语言?
· 学习不同编程语言的重要性
· 为什么我喜欢富于表达性的编程语言
· 计算机专业的女生为什么要学编程
· 前端必读:浏览器内部工作原理
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
 
Powered by:
博客园
Copyright © Jackey