重写DataGridViewTextBoxCell绘制方法

时间:2008-10-07 03:29:38

标签: c# winforms

我试图在派生类中覆盖DataGridViewTextBoxCell的paint方法,以便我可以通过一些可变数量的像素缩进前景文本。我希望如果列的宽度调整,使其总宽度是我的单元格文本的长度加上“缓冲区”缩进。有谁知道这样做的方法?我的蹩脚实现如下:

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

}

}

3 个答案:

答案 0 :(得分:2)

您可以在datagridview中连接到CellFormattingEvent并在那里进行格式化。或者,如果您从DataGridView中获取,则可以覆盖OnCellFormatting方法。代码看起来像这样:

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

只是一些粗略的代码,但你明白了。

答案 1 :(得分:2)

嗯,我想我拥有它。如果有人对此感兴趣,请输入以下代码:

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);
        }

}

如果有人有更好的方式,我很乐意看到你的解决方案。

答案 2 :(得分:1)

如果您尝试自动调整列的大小(取决于单元格内容的大小),那么您应该查看Column.AutoSizeMode属性和Column.DefaultCellStyle属性。

static const int INDENTCOEFF = 5;
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();

cellStyle.Padding = 
         new Padding(INDENTCOEFF , 5, INDENTCOEFF , 5); //left,top,right,bottom
MyColumn.DefaultCellStyle = cellStyle;
MyColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;