C# winform 做 datagridVIew 需要在一个 Cell 中添加两个按钮怎么搞?

2017-06-06 15:14:54 +08:00
 andychen20121130
1312 次点击
所在节点    问与答
1 条回复
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");
}
}
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/366368

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX