控制DataGridView的cell中具体字符的颜色
来源:互联网 发布:ge矩阵法 编辑:程序博客网 时间:2024/04/29 04:13
要实现此功能,首先我们就要知道DataGridView是怎么画单元格的,因此我们可以利用微软的这个事件来dataGridView1_CellPainting来实现我们要的效果:
具体代码如下:
//首先创建一个窗体:在Load事件中写入:int clength = 0; private void Form2_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("c1"); dt.Columns.Add("c2"); for (int j = 0; j < 10; j++) { dt.Rows.Add("你23ABI扩8873大", "bbbb"); } this.dataGridView1.DataSource = dt; for (int j = 0; j < 10; j++) { int height = TextRenderer.MeasureText( this.dataGridView1[0, j].Value.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width; this.dataGridView1.Rows[j].Height = height; } this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting); }
在接下来的代码中,将对中文,字母,数字通过不同的显示颜色来区分:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.Value != null) { e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground); clength = e.CellBounds.Location.X; foreach (char c in e.Value.ToString()) { if (char.IsDigit(c)) { int length = TextRenderer.MeasureText(c.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width; e.Graphics.DrawString(c.ToString(), this.dataGridView1.DefaultCellStyle.Font, new SolidBrush(Color.Red), clength, e.CellBounds.Location.Y+this.dataGridView1.Rows[e.RowIndex].Height/2-5); clength += Convert.ToInt32(length / 2); } else if (char.IsUpper(c)||char.IsLower(c)) { int length = TextRenderer.MeasureText(c.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width; e.Graphics.DrawString(c.ToString(), this.dataGridView1.DefaultCellStyle.Font, new SolidBrush(Color.Gold), clength, e.CellBounds.Location.Y + this.dataGridView1.Rows[e.RowIndex].Height / 2-5); clength += length / 2; } else { int length = TextRenderer.MeasureText(c.ToString(), this.dataGridView1.DefaultCellStyle.Font).Width; e.Graphics.DrawString(c.ToString(), this.dataGridView1.DefaultCellStyle.Font, new SolidBrush(Color.Green), clength, e.CellBounds.Location.Y + this.dataGridView1.Rows[e.RowIndex].Height / 2-5); clength += length / 2 + 4; } } e.Handled = true; } }效果如下:
- 控制DataGridView的cell中具体字符的颜色
- DataGridView中cell 值的控制
- DataGridView的Cell的字体颜色变化
- 在shell脚本文件中控制输出字符的颜色
- python 中输出字符的颜色控制属性
- .NET---DataGridView中DataGridViewComboBoxColumn类型的Cell赋值方法
- 交替颜色的DataGridView
- 交替颜色的DataGridView
- 设置DataGridView的颜色
- datagridview Cell中空值的处理
- 设置tableview的cell颜色
- 【POI】自定义cell的颜色
- 怎样在ios开发中设置tableview的cell颜色
- iOS中 cell的选中颜色以及默认选中行
- DataGridView改变行的颜色
- C#中如何改变DataGridView控件边框颜色的方法
- 改变DataGridView具体单元格的样式
- winform中改变DataGridView中符合条件的字体的颜色和列的颜色
- 使用ExtJS2构造动态异步加载的树
- 转载等级书购买注意事项
- 转载等级书购买注意事项
- 转载等级书购买注意事项
- 开博了
- 控制DataGridView的cell中具体字符的颜色
- 改版了?
- 如何识别优秀的程序员
- 面向对象度量和Lorenz检验规则
- 问
- 关于ppc8260的东西 写的挺好
- asp.net GridView中的事件触发
- 任正非优秀党员座谈会发言:不要试图做完人
- “天道酬勤”——任正非