Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > AutoSize row while typing.

Answered AutoSize row while typing.

Feed from this thread
  • Jack avatar

    Posted on Feb 3, 2012 (permalink)

    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.


    Reply

  • Answer Ivan Petrov Ivan Petrov admin's avatar

    Posted on Feb 8, 2012 (permalink)

    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 >>

    Reply

  • Jack avatar

    Posted on Feb 8, 2012 (permalink)

    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.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > AutoSize row while typing.
Related resources for "AutoSize row while typing."

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]