How to wrap text without spaces in a DataGridView (vb.net/C#)

I have already installed DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True

But this WrapModedoes not carry single-word columns without spaces. Is there a way we can β€œbreak” along with WrapMode? Or any other solution?

+4
source share
2 answers

You can play with the event CellPainting.

DrawStringevaluates the bounding Rectanglebox and wraps where it hits the right border.

, . FormattedValue, .

, .

private void DGV1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.Value == null) return;
    if (e.FormattedValue.GetType() != typeof( System.String) ) return;
    bool selected = (e.State & DataGridViewElementStates.Selected) 
                            == DataGridViewElementStates.Selected;
    string s = e.FormattedValue.ToString();

    //if (s.Length > 20) // Apply to all or only those breaking your limits
    {
        e.PaintBackground(e.CellBounds, selected);
        e.Graphics.DrawString(s, DGV1.Font, selected ? 
                   SystemBrushes.HighlightText : SystemBrushes.ControlText, 
                   new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 2, 
                                 e.CellBounds.Width - 2, e.CellBounds.Height - 4));
        e.Handled = true;
    }
}

Row.Heights . FormattedValue, RectangleF ; Height Cell. Row.Height, Row, .. , .. , / . , , ..

enter image description here

+2

hi Shimply DataBinding

DGLogs.Columns[0].DefaultCellStyle.WrapMode=DataGridViewTriState.True;

set
AutosizecolumnMode=AllCells
AutosizeRowMode=AllCells

, hown belo Image enter image description here

0

All Articles