AutoSize row while typing.

3 posts, 1 answers
  1. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 03 Feb 2012 Link to this post

    Hi,

    I have a RadGridVIew with a GridViewTextBoxColumn with AcceptsReturn, Multiline, WrapText properties set to true.
    When the user hits the return key after typing in a line in this column, I want the row height to increase. This way they can see what they have already typed in and continue typing in.

    I have set AutoSizeRows property on the RadGridView to true but this takes affect after editing the cell is finished.

    Please help in this regard.


  2. Answer
    Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 08 Feb 2012 Link to this post

    Hi Jack,

    Thank you for writing.

    The scenario you want to achieve can be done by changing the MinSize property of the editor. Here is a code snippet which achieves that:
    private int lineHeight = 0;
    private Size cacheMinSize;
     
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
      RadTextBoxEditor editor = e.ActiveEditor as RadTextBoxEditor;
      if (editor != null)
      {
        RadTextBoxEditorElement element = editor.EditorElement as RadTextBoxEditorElement;
     
        if (this.cacheMinSize != Size.Empty)
        {
          element.MinSize = this.cacheMinSize;
        }
     
        this.lineHeight = element.Font.Height;
        this.cacheMinSize = element.MinSize;
        element.TextChanged += new EventHandler(element_TextChanged);
      }
    }
     
    private void element_TextChanged(object sender, EventArgs e)
    {
      GridRowElement row = this.radGridView1.CurrentCell.RowElement;
      row.InvalidateMeasure();
      row.UpdateInfo();
     
      RadTextBoxEditorElement textBoxElement = (RadTextBoxEditorElement)sender;
      Size textSize = TextRenderer.MeasureText(textBoxElement.Text, textBoxElement.Font);
     
      textBoxElement.MinSize = new Size(textBoxElement.Size.Width, textSize.Height + textBoxElement.Padding.Vertical + this.lineHeight);
    }

    There is an issue when the user types in the editor and the row height increases the row starts to cover the rows below it. When you finish editing the rows layout is invalidated and all rows take the space they require without overlapping.

    I hope this will be useful for you. Should you have further questions, I would be glad to help.

    Kind regards,
    Ivan Petrov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 08 Feb 2012 Link to this post

    Hi Ivan,

    Thanks for the code snippet. I had to add the following line of code in the element_TextChanged event to get the desired functionality. 
    this.radGridView1.CurrentCell.MinSize = textBoxElement.MinSize;


    Thanks again.

Back to Top