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

大亮lu29
2013-10-10 · TA获得超过3386个赞
知道小有建树答主
回答量:977
采纳率:0%
帮助的人:697万
展开全部
在DataGridView的CellPainting事件中,加入如下代码privatevoid dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){ if (e.RowIndex >= 0 && e.ColumnIndex == -1){Rectangle newRect = newRectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Height, e.CellBounds.Height);//新建一个图标System.Drawing.Icon ico = newIcon("F:\\xx.ico");using (Brush gridBrush = newSolidBrush(this.dataGridView1.GridColor),backColorBrush = newSolidBrush(e.CellStyle.BackColor)){using (Pen gridLinePen = newPen(gridBrush,2)) { // Erase the cell. e.Graphics.FillRectangle(backColorBrush, e.CellBounds); //划线 Point p1 = newPoint(e.CellBounds.Left+e.CellBounds.Width, e.CellBounds.Top); Point p2 = newPoint(e.CellBounds.Left+e.CellBounds.Width, e.CellBounds.Top+e.CellBounds.Height); Point p3 = newPoint(e.CellBounds.Left,e.CellBounds.Top+ e.CellBounds.Height); Point[] ps = newPoint[]{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);}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式