V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  huashi2017  ›  全部回复第 1 页 / 共 1 页
回复总数  1
继承 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");
}
}
}
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6033 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 02:06 · PVG 10:06 · LAX 18:06 · JFK 21:06
Developed with CodeLauncher
♥ Do have faith in what you're doing.