C#如何在DataGridView的RowHeader显示字符串和图标

href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

【资料收集自网络】


在DataGridView的CellPainting事件中,加入如下代码

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{    href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

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事件中

href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

//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);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值