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

AutoSize row while typing.

2 Answers 129 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 1
Jack asked on 03 Feb 2012, 07:42 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Petrov
Telerik team
answered on 08 Feb 2012, 04:21 PM
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 >>
0
Jack
Top achievements
Rank 1
answered on 08 Feb 2012, 05:06 PM
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.

Tags
GridView
Asked by
Jack
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Jack
Top achievements
Rank 1
Share this question
or