如何禁用细胞的文本省略号在WindowsForms的DataGridView? [英] How to disable ellipsis of cell texts in a WindowsForms DataGridView?
问题描述
我在只读模式一个DataGridView在.NET 3.5(Visual Studio 2008中)WinForms应用程序。
I have a DataGridView in readonly mode in a .NET 3.5 (Visual Studio 2008) WinForms application.
的细胞的宽度是非常小的。一些单元包含一个短号码。现在,即使是用小字体,有时数显示为省略号。例如8 ......的而不是88的。
The cells' width is very small. Some of the cells contain a short number. Now, even with a small font, sometimes the number is shown with an ellipsis. For example "8..." instead of "88".
有没有办法让在一个标准的DataGridView在接下来的单元格中的文本流,避免省略号?
Is there a way to let the text flow over the next cell in a standard DataGridView and avoid the ellipsis?
谢谢!的
推荐答案
我发现这里由KD2ND给出的解决方案是不令人满意的。看来愚蠢全面重新实现电池绘画对于这样一个小的变化 - 这是大量的工作来处理涂装列标题及放大器;选定行了。幸运的是一个整洁的解决方案:
I found the solution given here by KD2ND to be unsatisfying. It seems silly to fully re-implement cell painting for such a small change - it's lots of work to handle painting of column headers & selected rows too. Luckily there is a neater solution:
// you can also handle the CellPainting event for the grid rather than
// creating a grid subclass as I have done here.
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
var isSelected = e.State.HasFlag(DataGridViewElementStates.Selected);
e.Paint(e.ClipBounds, DataGridViewPaintParts.Background
//| DataGridViewPaintParts.Border
//| DataGridViewPaintParts.ContentBackground
//| DataGridViewPaintParts.ContentForeground
| DataGridViewPaintParts.ErrorIcon
| DataGridViewPaintParts.Focus
| DataGridViewPaintParts.SelectionBackground);
using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor),
selectedForeBrush = new SolidBrush(e.CellStyle.SelectionForeColor))
{
if (e.Value != null)
{
StringFormat strFormat = new StringFormat();
strFormat.Trimming = StringTrimming.Character;
var brush = isSelected ? selectedForeBrush : foreBrush;
var fs = e.Graphics.MeasureString((string)e.Value, e.CellStyle.Font);
var topPos= e.CellBounds.Top + ((e.CellBounds.Height - fs.Height) / 2);
// I found that the cell text is drawn in the wrong position
// for the first cell in the column header row, hence the 4px
// adjustment
var leftPos= e.CellBounds.X;
if (e.RowIndex == -1 && e.ColumnIndex == 0) leftPos+= 4;
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
brush, leftPos, topPos, strFormat);
}
}
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border);
e.Handled = true;
}
诀窍就是让现有的`Paint方法处理的大部分细胞的画。我们只处理绘画的文字。后文则绘制边框,因为我发现,否则,文字有时会涂在边境,这很糟糕。
The trick is to let the existing `Paint method handle the painting of most of the cell. We only handle painting the text. The border is painted after the text because I found that otherwise, the text would sometimes be painted over the border, which looks bad.
这篇关于如何禁用细胞的文本省略号在WindowsForms的DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!