Cell padding doesn't work with WrapText

2 posts, 1 answers
  1. Elizabeth
    Elizabeth avatar
    3 posts
    Member since:
    Feb 2012

    Posted 12 Apr 2012 Link to this post

    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;
                }
            }
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 17 Apr 2012 Link to this post

    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 >>
Back to Top