logo 广告
Loading...
 导航 所在位置:论坛首页 -> ┈┋电脑网络┋┈ -> Asp/Asp.Net教程 -> C#如何在RowHeader显示字符串和图标
回复
标题:C#如何在RowHeader显示字符串和图标收藏 编辑 删除 楼主 | 上一篇 下一篇
晕菜两次
头像
等级:社区游民
权限:普通用户
积分:13
金钱:213
声望:16
经验:16
发帖数:47
注册:2008年4月20日
资料 短消息2008-6-7 10:49:51

COLOR: blue; FONT-FAMILY: 新宋体" twffan="done">  在DataGridView的CellPainting事件中,加入如下代码

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{    <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 11"> <meta name=Originator content="Microsoft Word 11">

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Height, e.CellBounds.Height);

//新建一个图标

System.Drawing.Icon ico = new Icon("F:\\xx.ico");

using (Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor))

{

using (Pen gridLinePen = new Pen(gridBrush,2))

        {

            // Erase the cell.

            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);        

 

            //划线

            Point p1 = new Point(e.CellBounds.Left+e.CellBounds.Width, e.CellBounds.Top);

            Point p2 = new Point(e.CellBounds.Left+e.CellBounds.Width, e.CellBounds.Top+e.CellBounds.Height);

            Point p3 = new Point(e.CellBounds.Left,e.CellBounds.Top+ e.CellBounds.Height);

            Point[] ps = new Point[]{p1,p2,p3};

            e.Graphics.DrawLines(gridLinePen,ps);

 

            //画图标

            e.Graphics.DrawIcon(ico, newRect);

            //画字符串

            e.Graphics.DrawString("123", e.CellStyle.Font, Brushes.Crimson, e.CellBounds.Left + 20, e.CellBounds.Top, StringFormat.GenericDefault);

            e.Handled = true;

          }

      }

}

}


 

 

 

  例外还有其他方法供参考,也是在DataGridView的CellPainting事件中

 //A显示在-1(即RowHeader)列之上

if (e.RowIndex >= 0 && e.ColumnIndex == 0)

{

e.Graphics.DrawString("A", this.dataGridView1.Font, Brushes.Red, 20, e.CellBounds.Top + 5);

}

 

//A则被-1(即RowHeader)列覆盖。

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

e.Graphics.DrawString("A", this.dataGridView1.Font, Brushes.Red, 20, e.CellBounds.Top + 5);

}

 

//XX图片在-1(即RowHeader)列之上

if (e.RowIndex >= 0 && e.ColumnIndex == 0)

{

Image img = Image.FromFile(@"F:\xx.bmp");

    e.Graphics.DrawImage(img, 0, e.CellBounds.Top);

}

 

//XX图片被-1(即RowHeader)列覆盖

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

Image img = Image.FromFile(@"F:\xx.bmp");

e.Graphics.DrawImage(img, 0, e.CellBounds.Top);

}

 

//XX图片被-1(即RowHeader)列覆盖

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

Image img = Image.FromFile(@"F:\xx.bmp");

e.Graphics.DrawImage(img, e.CellBounds.Left, e.CellBounds.Top);

}

 

//XX图片被0列覆盖

if (e.RowIndex >= 0 && e.ColumnIndex == 0)

{

Image img = Image.FromFile(@"F:\xx.bmp");

e.Graphics.DrawImage(img, e.CellBounds.Left, e.CellBounds.Top);

}

签名

2008-6-7 10:49:51 顶部
第1页 共页 共0个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com