源代码也在blog上发布,有什么问题可以留言
http://blog.csdn.net/Samen168/archive/2007/01/23/1491310.aspx 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;
}
} }
http://blog.csdn.net/Samen168/archive/2007/01/23/1491310.aspx 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;
}
} }
解决方案 »
- 正则问题
- 分离sql server数据库,跑哪里去了。分离后的.mdf数据库里面的数据会不会改变??
- 用bmp.Save("d:\\w.bmp")保存图像正常,而加图像格式后如bmp.Save(("d:\\w.bmp", .Bmp )保存则是黑色的背景,如何变成白色的背景呢?
- asp+SQL网站运行求助,急!
- 请问下
- 问题问题问题~ 应该较简单
- 关于将datagrid导入excel的问题
- 请教一个注册windows service问题?
- Microsoft .net框架程序设计
- 帮我看看了,这段代码的错误。50分
- 如何修改ACCESS库中的表名,在线等
- 各位看官,C#3.0中的Lambda 表达式
借宝地问个问题
winform中加了DataGridView等一些控件,
相對於dephi,pb的datawindow,我老觉得窗口绘制的較慢