Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding DataGridViewTextBoxCell Paint Method

Tags:

c#

winforms

I am trying to override the DataGridViewTextBoxCell's paint method in a derived class so that I can indent the foreground text by some variable amount of pixels. I would like it if the width of the column adjusts so that its total width is the length of my cells text plus the "buffer" indent. Does anyone know of a way to accomplish this? My lame implementation is listed below:

public class MyTextBoxCell : DataGridViewTextBoxCell{ ....
        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) {
           clipBounds.Inflate(100, 0);

            DataGridViewPaintParts pp = DataGridViewPaintParts.Background | DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentBackground
                | DataGridViewPaintParts.ErrorIcon;
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pp);            

                string text = formattedValue as string;

//My lame attempt to indent 20 pixels??
                TextRenderer.DrawText(graphics, text, cellStyle.Font, new Point(cellBounds.Location.X + 20, cellBounds.Location.Y), cellStyle.SelectionForeColor ,TextFormatFlags.EndEllipsis);

}

}

like image 577
sbeskur Avatar asked Sep 02 '26 03:09

sbeskur


2 Answers

You can just hook up to the CellFormattingEvent in the datagridview and do your formatting there. Or, if you're inherting from the DataGridView, you can just override the OnCellFormatting method. The code would look something like this:

            if (e.ColumnIndex == 1)
            {
                string val = (string)e.Value;
                e.Value = String.Format("   {0}", val);
                e.FormattingApplied = true;
            }

Just some rough code, but you get the idea.

like image 112
BFree Avatar answered Sep 04 '26 15:09

BFree


Well I think I have it. In case anyone is interested here is the code below:

public class MyTextBoxCell : DataGridViewTextBoxCell{ ....
        private static readonly int INDENTCOEFFICIENT = 5;
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) {
            int indent = ((MyRow)OwningRow).Indent * INDENTCOEFFICIENT;
            Size s =  base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);
            int textWidth = 2;  //arbitrary amount
            if (Value != null) {
                string text = Value as string;
                textWidth = TextRenderer.MeasureText(text, cellStyle.Font).Width;
            }

            s.Width += textWidth + indent;
            return s;
        }

        private static readonly StringFormat strFmt = new StringFormat(StringFormatFlags.NoWrap);

        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) {

            DataGridViewPaintParts pp = DataGridViewPaintParts.Background | DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentBackground
                | DataGridViewPaintParts.ErrorIcon;

            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pp);

            string text = formattedValue as string;

            int indent = ((MyRow)OwningRow).Indent * INDENTCOEFFICIENT;
            strFmt.Trimming = StringTrimming.EllipsisCharacter;
            Rectangle r = cellBounds;
            r.Offset(indent, 0);
            r.Inflate(-indent, 0);
            graphics.DrawString(text, cellStyle.Font, Brushes.Black, r, strFmt);
        }

}

If anyone has a better way I would love to see your solution.

like image 28
sbeskur Avatar answered Sep 04 '26 15:09

sbeskur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!