Rt:
The main methods used:
How to draw in cellpainting
private void Datagridview1_cellpainting (object sender, DataGridViewCellPaintingEventArgs e) { if (e.columnindex >= 0 && e.rowindex >= 0) {if (this.datagridview1.columns[e.co Lumnindex]. HeaderText = = "Op") {Employee model = This.datagridview1.rows[e.rowindex].
Databounditem as Employee; if (model!= null) {StringFormat SF = StringFormat.GenericDefault.Clone () as
StringFormat; sf.
FormatFlags = Stringformatflags.displayformatcontrol; sf.
Alignment = Stringalignment.center; sf.
LineAlignment = Stringalignment.center; sf.
trimming = Stringtrimming.ellipsischaracter;
E.paintbackground (E.cellbounds, false); int MD = model.
Age% 3;
string text = "Delete"; if (model.
Age% 3 = 1) {text = "Modify";
Switch (MD) {Case 1:
E.graphics.drawstring (text, This.dataGridView1.Font, brushes.red, E.cellbounds, SF);
Break
Case 2:string text2 = "delete"; if (model.
Age% 3 = 2) {Text2 = "modify";
} SizeF size = e.graphics.measurestring (text, This.dataGridView1.Font);
SizeF size2 = e.graphics.measurestring (Text2, This.dataGridView1.Font); float z = size. Width/(size. Width + size2.
Width); Float z2 = size2. Width/(size.Width + size2.
Width);
RectangleF rect1 = new RectangleF (E.cellbounds.left, e.cellbounds.top, E.cellbounds.width * z, e.cellbounds.height); RectangleF rect2 = new RectangleF (rect1.
Right, E.cellbounds.top, E.cellbounds.width * Z2, E.cellbounds.height);
E.graphics.drawstring (text, This.dataGridView1.Font, brushes.red, Rect1, SF);
E.graphics.drawstring (Text2, This.dataGridView1.Font, brushes.red, Rect2, SF);
Break Default:e.graphics.drawstring (text, This.dataGridView1.Font, brushes.red, E.cellbounds,
SF);
Break
} e.handled = true;
}
}
}
The action is processed in the Cellmouseclick event.
private void Datagridview1_cellmouseclick (object sender, DataGridViewCellMouseEventArgs e) {P
oint pt = e.location; if (e.columnindex >= 0 && e.rowindex >= 0) {if (this.datagridview1.columns[e.co Lumnindex]. HeaderText = = "Op") {Employee model = This.datagridview1.rows[e.rowindex].
Databounditem as Employee; if (model!= null) {int MD = model.
Age% 3;
string text = "Delete"; if (model.
Age% 3 = 1) {text = "Modify";
Switch (MD) {Case 1:
Break
Case 2:string text2 = "delete"; if (model.
Age% 3 = 2) {Text2 = "modify";
} Graphics g = This.dataGridView1.CreateGraphics ();
SizeF size = g.measurestring (text, This.dataGridView1.Font);
SizeF size2 = g.measurestring (Text2, This.dataGridView1.Font); float z = size. Width/(size. Width + size2.
Width); Float z2 = size2. Width/(size. Width + size2.
Width); Rectangle rect = new Rectangle (0, 0, This.datagridview1.columns[e.columnindex]. Width, This.datagridview1.rows[e.rowindex].
Height); RectangleF rect1 = new RectangleF (rect. Left, rect. Top, Rect. Width * Z, rect.
Height); RectangleF rect2 = new RectangleF (rect1. Right, rect. Top, Rect. Width * Z2, rect.
Height); if (rect1.
Contains (PT)) {MessageBox.Show ("1111111"); } if (Rect2.
Contains (PT)) {MessageBox.Show ("2222222");
} break;
Default:break; }
}
}
}
}
Image above:
Source: http://download.csdn.net/detail/zanfeng/9453608