如何禁用 WindowsForms DataGridView 中单元格文本的省略号?

发布于 2024-07-20 19:22:59 字数 253 浏览 9 评论 0原文

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

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

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

谢谢!

I have a DataGridView in readonly mode in a .NET 3.5 (Visual Studio 2008) WinForms application.

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".

Is there a way to let the text flow over the next cell in a standard DataGridView and avoid the ellipsis?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撩动你心 2024-07-27 19:22:59

在设计器中更改 DataGridView 属性“RowDefaultCellStyle”-> 设置“换行模式”=“真”

In Designer change DataGridView Property "RowDefaultCellStyle" -> set "Wrap Mode" = "true"

森罗 2024-07-27 19:22:59

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

问候

handle the DataGridView control's CellPainting event.
Check the following link :

http://msdn.microsoft.com/en-us/library/hta8z9sz.aspx

Note that when you draw the text itself you need to customize the StringFormat -

quote from the MSDN code :

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);
}

Use the following StringFormat object instead of StringFormat.GenericDefault :

StringFormat strFormat = new StringFormat();
strFormat.Trimming = StringTrimming.None;

Regards

旧夏天 2024-07-27 19:22:59

我发现 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”方法处理大部分单元格的绘制。 我们只处理文本的绘制。 边框是在文本之后绘制的,因为我发现否则有时文本会被绘制在边框上,这看起来很糟糕。

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;
}

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.

对你的占有欲 2024-07-27 19:22:59

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

No, there's probably some property to disable the ellipsis (if you access the underlying controls), but flow over (and also cell merging) is not supported in the standard DataGridView.

乜一 2024-07-27 19:22:59

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

A simple technique that might work for you is just to turn WrapMode for the cell in question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文