V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
andychen20121130
V2EX  ›  问与答

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

  •  
  •   andychen20121130 · 2017-06-06 15:14:54 +08:00 · 1326 次点击
    这是一个创建于 2759 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1 条回复    2017-06-07 08:26:24 +08:00
    huashi2017
        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");
    }
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   980 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 21:08 · PVG 05:08 · LAX 13:08 · JFK 16:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.