6

我在 .NET 3.5 (Visual Studio 2008) WinForms 应用程序中有一个只读模式的 DataGridView。

单元格的宽度非常小。一些单元格包含一个短数字。现在,即使使用小字体,有时数字也会用省略号显示。例如"8..."而不是"88"

有没有办法让文本流过标准 DataGridView 中的下一个单元格并避免省略号?

谢谢!

4

5 回答 5

4

在设计器中更改 DataGridView 属性 "RowDefaultCellStyle" -> 设置 "Wrap Mode" = "true"

于 2014-10-23T23:36:34.850 回答
3

处理 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;

问候

于 2010-05-12T11:03:51.513 回答
2

我发现 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;
}

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.

于 2015-04-14T17:03:56.333 回答
1

不,可能有一些属性可以禁用省略号(如果您访问底层控件),但标准 DataGridView 不支持流过(以及单元格合并)。

于 2009-05-26T11:50:08.307 回答
1

一个可能对您有用的简单技术就是为有问题的单元格打开 WrapMode

于 2014-01-06T08:02:26.730 回答