首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重写DataGridViewTextBoxCell绘制方法

重写DataGridViewTextBoxCell绘制方法
EN

Stack Overflow用户
提问于 2008-10-07 03:29:38
回答 3查看 4.4K关注 0票数 3

我试图在派生类中重写DataGridViewTextBoxCell的paint方法,以便可以按可变像素量缩进前景文本。我希望调整列的宽度,使其总宽度为我的单元格文本的长度加上“缓冲区”缩进量。有没有人知道有什么方法可以做到这一点?我的lame实现如下所示:

代码语言:javascript
复制
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);

}

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2008-10-07 21:14:53

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

代码语言:javascript
复制
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;
票数 1
EN

Stack Overflow用户

发布于 2008-10-07 03:53:20

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

代码语言:javascript
复制
            if (e.ColumnIndex == 1)
            {
                string val = (string)e.Value;
                e.Value = String.Format("   {0}", val);
                e.FormattingApplied = true;
            }

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

票数 2
EN

Stack Overflow用户

发布于 2008-10-07 20:35:09

嗯,我想我找到了。如果有人感兴趣,请看下面的代码:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/177133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档