正在重写DataGridViewTextBoxCell绘制方法
本文关键字:绘制 方法 DataGridViewTextBoxCell 重写 | 更新日期: 2023-09-27 17:47:46
我正试图在派生类中重写DataGridViewTextBoxCell的绘制方法,以便可以将前景文本缩进一些可变的像素量。如果列的宽度进行调整,使其总宽度为单元格文本的长度加上"缓冲区"缩进,我会喜欢它。有人知道实现这一点的方法吗?我的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;