C# 重写DataGridViewTextBoxCell绘制方法

C# 重写DataGridViewTextBoxCell绘制方法,c#,winforms,C#,Winforms,我试图在派生类中重写DataGridViewTextBoxCell的paint方法,以便可以将前景文本缩进一些可变数量的像素。我希望它,如果列的宽度调整,使其总宽度是我的单元格文本的长度加上“缓冲区”缩进。有人知道实现这一目标的方法吗?我的lame实现如下所示: public class MyTextBoxCell : DataGridViewTextBoxCell{ .... protected override void Paint(Graphics graphics, Re

我试图在派生类中重写DataGridViewTextBoxCell的paint方法,以便可以将前景文本缩进一些可变数量的像素。我希望它,如果列的宽度调整,使其总宽度是我的单元格文本的长度加上“缓冲区”缩进。有人知道实现这一目标的方法吗?我的lame实现如下所示:

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

}

}

您只需连接到datagridview中的CellFormattingEvent并在那里进行格式化即可。或者,如果您是从DataGridView导入,您可以只重写OnCellFormatting方法。代码如下所示:

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

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

我想我知道了。如果有人感兴趣,请参阅以下代码:

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

}

如果有人有更好的方法,我希望看到您的解决方案。

如果您试图自动调整列的大小(取决于单元格内容的大小),那么您应该查看
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;

B自由,我想避免在文本前面加空格。如果我可以缩进一个像素的数量渲染效果将不会丢失时,字体改变。也希望在单元级别处理此问题。这在其他网格产品中是可能的,我只是想知道如何用DataGridView实现这一点。Vivek,谢谢你指出Padding属性,我一开始就觉得错过了它很傻。我可以使用这种技术简化我的代码。DataGridView控件是我见过的最复杂的控件。很容易忽略它的一些简洁的特性。MyRow在代码中是什么类型的?你能把那个密码也发过来吗?