This is a migrated thread and some comments may be shown as answers.

Cell padding doesn't work with WrapText

1 Answer 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Elizabeth
Top achievements
Rank 1
Elizabeth asked on 12 Apr 2012, 09:50 PM
Hello,

I would like to have my gridview cell contents wrap. When I set the column WrapText = true there is no padding around the cells and the text hits the border.

I tried setting the cell padding using the CellFormatting event but then the WrapText doesn't happen and the cell uses ellipse.

Can I have both?

Thanks,
Beth

This does not work

void radGridView_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            e.CellElement.Padding = new Padding(3, 3, 5, 3);
            if (e.ColumnIndex != 0)
            {
                e.Column.WrapText = true;
            }
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 17 Apr 2012, 03:29 PM
Hello Elizabeth,

Thank you for writing.

In order to allow the wrapping functionality to be applied correctly, you need to set the AutoSizeRows property of RadGridView to true. This will allow the grid to size its rows according to its cells size. In regards to the padding, it works correctly in this scenario. Here is a sample app:
public Form1()
{
    InitializeComponent();
 
    Random r = new Random();
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Bool", typeof(bool));
    table.Columns.Add("DateColumn", typeof(DateTime));
 
    for (int i = 0; i < 10; i++)
    {
        table.Rows.Add(i, "Some long long text" + i, r.Next(10) > 5 ? true : false, DateTime.Now.AddHours(i));
    }
 
    this.radGridView1.DataSource = table;
    radGridView1.AutoSizeRows = true;
 
    radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(radGridView1_CellFormatting);
}
 
void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    e.CellElement.Padding = new Padding(3, 3, 5, 3);
    if (e.ColumnIndex != 0)
    {
        e.Column.WrapText = true;
    }
}

I hope this helps.
 
Kind regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Elizabeth
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or