1、CellFormatting事件,一般重繪單元格屬性。
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
private void dataGridVIEw1_CellFormatting(object sender,
System.Windows.Forms.DataGridVIEwCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridVIEw1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridVIEw1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
if (this.dataGridVIEw1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
if (this.nextcol != null & e.ColumnIndex == this.nextcol)
{
e.CellStyle.BackColor = Color.LightBlue;
this.nextcol = null;
}
if (this.nextrow != null & e.RowIndex == nextrow)
{
e.CellStyle.BackColor = Color.LightPink;
this.nextrow = null;
}
if (e.RowIndex != this.dataGridVIEw1.RowCount - 1)
{
if (e.Value.ToString() == this.dataGridVIEw1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
e.CellStyle.BackColor = Color.LightPink;
nextrow = e.RowIndex + 1;
}
}
}
if (this.dataGridVIEw1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
if (e.Value.ToString() == this.dataGridVIEw1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
{
e.CellStyle.BackColor = Color.LightBlue;
nextcol = e.ColumnIndex + 1;
}
}
}
//==========================
//繪制單元格
private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridVIEwCellPaintingEventArgs e)
{
//縱向合並
if (this.dataGridVIEw1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(this.dataGridVIEw1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原單元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
////繪制線條,這些線條是單元格相互間隔的區分線條,
////因為我們只對列name做處理,所以datagridvIEw自己會處理左側和上邊緣的線條
if (e.RowIndex != this.dataGridVIEw1.RowCount - 1)
{
if (e.Value.ToString() != this.dataGridVIEw1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
; {
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下邊緣的線
//繪制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
e.Handled = true;
}
}
}
//橫向合並
if (this.dataGridVIEw1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(this.dataGridVIEw1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原單元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.Value.ToString() != this.dataGridVIEw1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
{
//右側的線 //下邊緣的線 } } #endregion
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
//繪制值
if (e
.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}