Wrapping text in a DataGrid Cell

R

Randy

Hello,
How would this be done? I've looked at the datagrid itself and also the
tableStyle and can't find out how to make text wrap in a cell.
Thanks
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Randy,

When a cell is not being edited, its conent is painted with the
corresponding DataGridColumnStyle's Paint method (there are several
overloads). When it is being edited, cell appearence is in hands of a user
control used to edit the cell contents - most likely this will be a TextBox.

So what you need is inheriting from
System.Windows.Forms.DataGridTextBoxColumn and overriding its Paint method
to enable wrapping. You might also need to overload column style methods
responsible for calculating the row height - the grid won't be able to
adjust the row height upon double-click between row headers otherwise. To
make the text wrap during editing, you will need to tweak the hosted
TextBox - probably it should be switched to the MultiLine mode, but I am not
so sure here.
 
M

Michael Earls

Hi Randy,

The best way to solve any layout problem is to use CSS.

Set the CSS class of the cell to a CSS class that has the following
definition:

..nowrap{
white-space: nowrap;
}


or, you could set the inline style attribute to style="white-space:
nowrap;", but this is not supported in older Netscape browsers.

Look at the cell columns properties and set the class there if at all
possible.

Michael Earls
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top