我有一个.NET 3.5(Visual Studio 2008)WinForms应用程序在只读模式DataGridView。如何禁用WindowsForms DataGridView中单元格文本的省略号?
单元格的宽度非常小。一些单元格包含一个短号码。现在,即使使用小字体,有时数字也会以省略号显示。例如“8 ...”而不是“88”。
有没有办法让文本流过标准DataGridView中的下一个单元并避免省略号?
谢谢!
我有一个.NET 3.5(Visual Studio 2008)WinForms应用程序在只读模式DataGridView。如何禁用WindowsForms DataGridView中单元格文本的省略号?
单元格的宽度非常小。一些单元格包含一个短号码。现在,即使使用小字体,有时数字也会以省略号显示。例如“8 ...”而不是“88”。
有没有办法让文本流过标准DataGridView中的下一个单元并避免省略号?
谢谢!
我发现这里给出的解决方案KD2ND是不令人满意的。对于如此小的改变来完全重新实现单元绘画似乎很愚蠢 - 处理所选行的列标题的绘制也很麻烦。幸运的是,有一个整洁的解决方案:
// 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方法处理的大多数细胞的画。我们只处理绘画文本。边框是在文字后面绘制的,因为我发现否则,文本有时会涂在边框上,看起来很糟糕。
不,可能会有一些属性禁用省略号(如果您访问底层控件),但标准DataGridView不支持流过(和单元合并)。
处理DataGridView控件的CellPainting事件。 查看以下链接:
http://msdn.microsoft.com/en-us/library/hta8z9sz.aspx
注意,当您绘制的文本本身,你需要定制的StringFormat - 从MSDN代码
报价:
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
使用以下StringFormat对象而不是StringFormat.GenericDefault:
StringFormat strFormat = new StringFormat();
strFormat.Trimming = StringTrimming.None;
个
问候
一个简单的技术,可能会为你工作只是为了在问题
转WrapMode的电池在设计变化的DataGridView属性“RowDefaultCellStyle” - >设置“自动换行模式” =“真”