自定义DataGridViewButtonCell上的右对齐按钮

时间:2018-11-19 12:39:49

标签: c# winforms

我想创建一个自定义DataGridViewCell的示例

enter image description here

我开始创建此单元格。首先,我继承自DataGridViewButtonCell,并覆盖了重要的方法。

    private class DataGridViewAllocationCell : DataGridViewButtonCell
    {
        public void Initialize() // Pseudo Constructor with some arguments
        {
            contextMenu = new ContextMenuStrip();
            // fill the contextMenu here
        }

        private ContextMenuStrip contextMenu;

        private const string BUTTON_TEXT = "...";

        private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
        private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
            Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
            Rectangle cellRectangle = GetContentBounds(rowIndex);
            Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
            cellRectangle.Offset(displayRectangle.Location);
            base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
            TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
        }

        protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
        {
            Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
            return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);
        }

        protected override void OnContentClick(DataGridViewCellEventArgs e)
        {
            base.OnContentClick(e);
            Rectangle contentRectangle = GetContentBounds(e.RowIndex);
            Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
            Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
            contextMenu.Show(DataGridView, location);
        }
    }

使用这些单元格创建列时,我会看到此网格

enter image description here

重要的部分是第二列。按钮控件将填充单元格的其余部分。

有没有办法使按钮与文本一样大(默认宽度)并在右侧对齐?

1 个答案:

答案 0 :(得分:1)

我建议您采用覆盖DataGridViewTextBoxCell并在焦点对准时插入按钮的方法,而不是覆盖DataGridViewButtonCell并绘制文本部分。

A,您所做的仍然有效,如果您希望按钮以不同的方式对齐,您只需指定不同的内容范围即可,而不是:

return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);

您可以放置​​类似这样的内容,即一个22像素宽的右对齐按钮:

return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);

您的按钮的行为将与您期望的典型“ ...”按钮类似。无论如何,这就是解决问题的关键。