How to disable ellipsis of cell texts in a WindowsForms DataGridView?
Asked Answered
M

5

6

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!

Mella answered 26/5, 2009 at 11:30 Comment(0)
S
2

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.

Selden answered 14/4, 2015 at 17:3 Comment(0)
H
4

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

Hatpin answered 23/10, 2014 at 23:36 Comment(0)
I
3

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

Izolaiztaccihuatl answered 12/5, 2010 at 11:3 Comment(0)
S
2

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.

Selden answered 14/4, 2015 at 17:3 Comment(0)
B
1

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.

Burdick answered 26/5, 2009 at 11:50 Comment(0)
D
1

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

Diverse answered 6/1, 2014 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.