C#如何在DataGridView的RowHeader显示字符串和图标
2008-06-04 10:56
274 查看
【资料收集自网络】
在DataGridView的CellPainting事件中,加入如下代码
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
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);
}
在DataGridView的CellPainting事件中,加入如下代码
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
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);
}
相关文章推荐
- c#.winform,datagridview,数组,绑定,字符串,字符串数组绑定datagridview显示,长度,显示数组内容
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- C# WinFrom 中如何将txt内容显示到dataGridView
- C#如何使用dataGridView控件显示类对象数据
- C# .NET 如何在DataGridView中最简洁的实现对单元格显示的处理
- C#关于如何在任务栏中隐藏和显示其它应用程序的图标
- [c#]如何使datagridview查询结果显示为别名(非数据库列名)
- 关于C#中货币类型和数值类型、字符串类型的转化和在DataGridView中的显示问题:
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- C#.NET常见问题(FAQ)-如何使用DataGridView跟Excel数据交互
- 如何将无法显示的字符串用。代替
- Android APP如何设置显示图标
- C# 如何在textbox里显示txt文件的内容
- C#实现在combobox显示图标的方法
- C#点击窗体最小化时,隐藏任务栏和显示托盘图标
- C#.net格式化时间字符串达到不同的显示效果
- C#连接access在datagridview中显示
- C# 如何实现带消息数的程序桌面图标
- C# 如何使自己的程序在“显示桌面”时不最小化
- 如何让EasyUI的Tree或者ComboTree节点不显示图标?