关于VS2005 WinForm下DataGridView单元格合并,背景色设置
来源:互联网 发布:淘宝代金券用不了 编辑:程序博客网 时间:2024/06/01 12:47
最近发现这方面在社区内提问的不少,发个源代码,嘿嘿
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
dt.Columns.Add("D");
dt.Rows.Add(new object[] { "A1", "B1", "C1", "D1" });
dt.Rows.Add(new object[] { "A2", "B2", "C2", "D2" });
dt.Rows.Add(new object[] { "A3", "B3", "C3", "D3" });
this.dataGridView1.DataSource = dt;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView1.Rows.Count - 1 || e.ColumnIndex == -1)
{
return;
}
// 假定需要将C列值为C3的单元格与前一单元格合并
if (e.ColumnIndex == 1 && this.dataGridView1[e.RowIndex, e.ColumnIndex + 1].Value.ToString() == "C3")
{
e.Handled = true;
}
if (e.ColumnIndex == 2 && e.Value.ToString() == "C3")
{
DataGridViewCell preCell = this.dataGridView1[e.ColumnIndex -1, e.RowIndex];
Rectangle re = new Rectangle(e.CellBounds.Left - this.dataGridView1.Columns[e.ColumnIndex - 1].Width
, e.CellBounds.Top, e.CellBounds.Width + this.dataGridView1.Columns[e.ColumnIndex - 1].Width, e.CellBounds.Height);
e.Graphics.FillRectangle(Brushes.White, re);
Pen pen = new Pen(this.dataGridView1.BackgroundColor,1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawLine(pen, re.X, re.Y + re.Height-1, re.X + re.Width, re.Y + re.Height-1);
e.Graphics.DrawLine(pen, re.X + re.Width -1, re.Y , re.X + re.Width -1, re.Y + re.Height);
SizeF strSize = e.Graphics.MeasureString(e.Value.ToString(), this.dataGridView1.Font);
e.Graphics.DrawString(e.Value.ToString(), this.dataGridView1.Font
, Brushes.Black, re.X, re.Y + (re.Height - strSize.Height) / 2);
e.Handled = true;
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.RowIndex != -1 && e.ColumnIndex == 1 && e.Value != null && e.Value.ToString() == "B1")
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.Blue;
e.CellStyle.SelectionBackColor = Color.BlueViolet;
}
}
}
- 关于VS2005 WinForm下DataGridView单元格合并,背景色设置
- 关于VS2005 WinForm下DataGridView单元格合并,背景色设置
- 设置单元格背景色
- [WinForm]dataGridView背景色交替
- jxl 设置excel 字体 背景色 合并单元格 列宽
- jxl操作excel(合并单元格,设置背景色,字体颜色)
- jxl 设置excel 字体 背景色 合并单元格 列宽
- jxl 设置excel 字体 背景色 合并单元格 列宽
- fullCalendar设置单元格背景色
- POI 设置单元格背景色
- C# Winform DataGridView单元格合并
- winform的DataGridView合并单元格
- Excel2010关于设置单元格颜色和背景色的bug
- 设置vs2005的背景色
- [VB.NET]如何给DATAGRIDVIEW控件中指定的单元格设置背景色????
- 导出Excel示例(C++)【设置边框、设置背景色、合并单元格的示例】
- Winform程序中设置DataGridView的列标题背景色不起作用怎么办
- Winform程序中设置DataGridView的列标题背景色不起作用怎么办
- CString 与 char 与 BYTE 之间的转换
- 你的信用卡最低还款额是多少?
- DataGridView新特色(vs2005)
- 关于LD_LIBRARY_PATH
- 【转贴】程序员修身养性的十大原则
- 关于VS2005 WinForm下DataGridView单元格合并,背景色设置
- c#.net 中生成GUID
- .NET学习笔记
- Visual Studio 2005(C#)中只允许一个实例运行的一种方法
- 你的信用卡是什么颜色?由buyblue.org乱侃美国公司的红与蓝
- 使用java.nio读取网页的程序
- 美国房市还能火多久?看看最新预测怎么说。
- DropDownList绑定枚举类型
- 分析:华为辞职门事件赔了夫人又折兵