huashi2017
2017-06-07 08:26:24 +08:00
继承 DataGridViewCell 自己写一个 cell 类型出来,在里边画··· 这么做真的很麻烦很复杂···,一定要两个按钮完全可以用自带的 buttoncell 类 然后通过合并单元格的形式实现两个按钮的效果
class twobuttoncell : DataGridViewCell
{
public twobuttoncell():base()
{
}
public override Type FormattedValueType
{
get
{
Type valueType = base.ValueType;
if (valueType != null)
{
return valueType;
}
return typeof(String);
}
}
Rectangle lb;
Rectangle rb;
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
lb = new Rectangle(new Point(cellBounds.X+2,cellBounds.Y+2), new Size(cellBounds.Width / 2-4, cellBounds.Height-4));
rb = new Rectangle(new Point(cellBounds.X+cellBounds.Width/2+2,cellBounds.Y+2), new Size(cellBounds.Width / 2-4, cellBounds.Height-4));
graphics.FillRectangle(Brushes.White, cellBounds);
graphics.DrawRectangle(new Pen(cellStyle.ForeColor), lb);
graphics.DrawString("left", cellStyle.Font, Brushes.Red, lb);
graphics.DrawRectangle(new Pen(cellStyle.ForeColor), rb);
graphics.DrawString("right", cellStyle.Font, Brushes.Red, rb);
}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//base.OnMouseClick(e);
var rec = this.DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Point pt = new Point(rec.Location.X+e.X,rec.Y+e.Y);
if (lb.Contains(pt))
{
MessageBox.Show("left");
}
if (rb.Contains(pt))
{
MessageBox.Show("right");
}
}
}