|
| 1 | +--- |
| 2 | +title: How to AutoSize GridView's Row While Editing |
| 3 | +description: Learn how to autosize RadGridView's row while editing a multiline editor. |
| 4 | +type: how-to |
| 5 | +page_title: How to AutoSize GridView's Row While Editing |
| 6 | +slug: row-autosizing-while-editing |
| 7 | +position: 5 |
| 8 | +tags: grid, autosize, edit, multiline |
| 9 | +ticketid: 1521435 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +## Environment |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2021.2.511|RadGridView|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +The RadGridView.**AutoSizeRows** property controls whether row's height in a **RadGridView** will expand for multiline cell text if the column is wrapped. However, note that when a cell is in edit mode and you are typing in the editor, this value is not committed to the cell yet. That is why the row's height is not updated until you commit the editor's value: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +This solution demonstrates how to disable the rows auto sizing when the editor is activated and the row's height is adjusted by the **MinHeight** property in order to ensure that the whole multiline text inside the editor is visible: |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +````C# |
| 33 | + |
| 34 | +public RadForm1() |
| 35 | +{ |
| 36 | + InitializeComponent(); |
| 37 | + |
| 38 | + GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Description"); |
| 39 | + this.radGridView1.Columns.Add(textColumn); |
| 40 | + textColumn.WrapText = true; |
| 41 | + this.radGridView1.AutoSizeRows = true; |
| 42 | + this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; |
| 43 | + |
| 44 | + this.radGridView1.BeginUpdate(); |
| 45 | + for (int i = 0; i < 10; i++) |
| 46 | + { |
| 47 | + this.radGridView1.Rows.Add(i + "0.Data" + Environment.NewLine + i + "1.Data"); |
| 48 | + } |
| 49 | + this.radGridView1.EndUpdate(); |
| 50 | + |
| 51 | + this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized; |
| 52 | + this.radGridView1.CellEndEdit += radGridView1_CellEndEdit; |
| 53 | +} |
| 54 | + |
| 55 | +private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e) |
| 56 | +{ |
| 57 | + this.radGridView1.AutoSizeRows = true; |
| 58 | +} |
| 59 | + |
| 60 | +private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) |
| 61 | +{ |
| 62 | + this.radGridView1.AutoSizeRows = false; |
| 63 | + RadTextBoxEditor tbEditor = e.ActiveEditor as RadTextBoxEditor; |
| 64 | + if (tbEditor != null) |
| 65 | + { |
| 66 | + tbEditor.Multiline = true; |
| 67 | + tbEditor.AcceptsReturn = true; |
| 68 | + RadTextBoxEditorElement el = tbEditor.EditorElement as RadTextBoxEditorElement; |
| 69 | + if (el != null) |
| 70 | + { |
| 71 | + if (textHeight == -1) |
| 72 | + { |
| 73 | + using (Graphics g = el.TextBoxItem.TextBoxControl.CreateGraphics()) |
| 74 | + { |
| 75 | + textHeight = TextRenderer.MeasureText(g, el.TextBoxItem.HostedControl.Text.Substring(0,el.Text.IndexOf(Environment.NewLine)), el.TextBoxItem.HostedControl.Font).Height; |
| 76 | + } |
| 77 | + } |
| 78 | + el.TextBoxItem.TextBoxControl.TextChanged -= TextBoxControl_TextChanged; |
| 79 | + el.TextBoxItem.TextBoxControl.TextChanged += TextBoxControl_TextChanged; |
| 80 | + |
| 81 | + this.radGridView1.CurrentRow.MinHeight = this.radGridView1.CurrentRow.Height = ( el.TextBoxItem.TextBoxControl.Lines.Length+1)* textHeight; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +int textHeight = -1; |
| 87 | + |
| 88 | +private void TextBoxControl_TextChanged(object sender, EventArgs e) |
| 89 | +{ |
| 90 | + HostedTextBoxBase tb = sender as HostedTextBoxBase; |
| 91 | + if (tb != null) |
| 92 | + { |
| 93 | + this.radGridView1.CurrentRow.MinHeight = this.radGridView1.CurrentRow.Height=( tb.Lines.Length+1)* textHeight; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +```` |
| 99 | +````VB.NET |
| 100 | + |
| 101 | +Public Sub New() |
| 102 | + InitializeComponent() |
| 103 | + Dim textColumn As GridViewTextBoxColumn = New GridViewTextBoxColumn("Description") |
| 104 | + Me.RadGridView1.Columns.Add(textColumn) |
| 105 | + textColumn.WrapText = True |
| 106 | + Me.RadGridView1.AutoSizeRows = True |
| 107 | + Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill |
| 108 | + Me.RadGridView1.BeginUpdate() |
| 109 | + |
| 110 | + For i As Integer = 0 To 10 - 1 |
| 111 | + Me.RadGridView1.Rows.Add(i & "0.Data" & Environment.NewLine & i & "1.Data") |
| 112 | + Next |
| 113 | + |
| 114 | + Me.RadGridView1.EndUpdate() |
| 115 | + AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf radGridView1_CellEditorInitialized |
| 116 | + AddHandler Me.RadGridView1.CellEndEdit, AddressOf radGridView1_CellEndEdit |
| 117 | +End Sub |
| 118 | + |
| 119 | +Private Sub radGridView1_CellEndEdit(ByVal sender As Object, ByVal e As GridViewCellEventArgs) |
| 120 | + Me.RadGridView1.AutoSizeRows = True |
| 121 | +End Sub |
| 122 | + |
| 123 | +Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs) |
| 124 | + Me.RadGridView1.AutoSizeRows = False |
| 125 | + Dim tbEditor As RadTextBoxEditor = TryCast(e.ActiveEditor, RadTextBoxEditor) |
| 126 | + |
| 127 | + If tbEditor IsNot Nothing Then |
| 128 | + tbEditor.Multiline = True |
| 129 | + tbEditor.AcceptsReturn = True |
| 130 | + Dim el As RadTextBoxEditorElement = TryCast(tbEditor.EditorElement, RadTextBoxEditorElement) |
| 131 | + |
| 132 | + If el IsNot Nothing Then |
| 133 | + |
| 134 | + If textHeight = -1 Then |
| 135 | + |
| 136 | + Using g As Graphics = el.TextBoxItem.TextBoxControl.CreateGraphics() |
| 137 | + textHeight = TextRenderer.MeasureText(g, el.TextBoxItem.HostedControl.Text.Substring(0, _ |
| 138 | + el.Text.IndexOf(Environment.NewLine)), el.TextBoxItem.HostedControl.Font).Height |
| 139 | + End Using |
| 140 | + End If |
| 141 | + |
| 142 | + RemoveHandler el.TextBoxItem.TextBoxControl.TextChanged, AddressOf TextBoxControl_TextChanged |
| 143 | + AddHandler el.TextBoxItem.TextBoxControl.TextChanged, AddressOf TextBoxControl_TextChanged |
| 144 | + |
| 145 | + Me.RadGridView1.CurrentRow.MinHeight = (el.TextBoxItem.TextBoxControl.Lines.Length + 1) * textHeight |
| 146 | + Me.RadGridView1.CurrentRow.Height = (el.TextBoxItem.TextBoxControl.Lines.Length + 1) * textHeight |
| 147 | + End If |
| 148 | + End If |
| 149 | +End Sub |
| 150 | + |
| 151 | +Private textHeight As Integer = -1 |
| 152 | + |
| 153 | +Private Sub TextBoxControl_TextChanged(ByVal sender As Object, ByVal e As EventArgs) |
| 154 | + Dim tb As HostedTextBoxBase = TryCast(sender, HostedTextBoxBase) |
| 155 | + |
| 156 | + If tb IsNot Nothing Then |
| 157 | + Me.RadGridView1.CurrentRow.MinHeight = (tb.Lines.Length + 1) * textHeight |
| 158 | + Me.RadGridView1.CurrentRow.Height = (tb.Lines.Length + 1) * textHeight |
| 159 | + End If |
| 160 | +End Sub |
| 161 | + |
| 162 | +```` |
| 163 | + |
| 164 | +However, if there is not any specific validation logic and it is not a problem to commit the editor's value immediately after typing, it is possible to use a very simple solution by handling the **ValueChanging** event that RadGridView offers and updating the cell's value accordingly. Thus, you can keep the rows autositing enabled: |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +````C# |
| 169 | +public RadForm1() |
| 170 | +{ |
| 171 | + InitializeComponent(); |
| 172 | + |
| 173 | + GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Description"); |
| 174 | + this.radGridView1.Columns.Add(textColumn); |
| 175 | + textColumn.WrapText = true; |
| 176 | + this.radGridView1.AutoSizeRows = true; |
| 177 | + this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; |
| 178 | + |
| 179 | + this.radGridView1.BeginUpdate(); |
| 180 | + for (int i = 0; i < 10; i++) |
| 181 | + { |
| 182 | + this.radGridView1.Rows.Add(i + "0.Data" + Environment.NewLine + i + "1.Data"); |
| 183 | + } |
| 184 | + this.radGridView1.EndUpdate(); |
| 185 | + |
| 186 | + this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized; |
| 187 | + |
| 188 | + this.radGridView1.ValueChanging += radGridView1_ValueChanging; |
| 189 | +} |
| 190 | + |
| 191 | +private void radGridView1_ValueChanging(object sender, ValueChangingEventArgs e) |
| 192 | +{ |
| 193 | + this.radGridView1.CurrentCell.Value = e.NewValue; |
| 194 | +} |
| 195 | + |
| 196 | +private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) |
| 197 | +{ |
| 198 | + RadTextBoxEditor tbEditor = e.ActiveEditor as RadTextBoxEditor; |
| 199 | + if (tbEditor != null) |
| 200 | + { |
| 201 | + tbEditor.Multiline = true; |
| 202 | + tbEditor.AcceptsReturn = true; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +```` |
| 207 | +````VB.NET |
| 208 | +Public Sub New() |
| 209 | + InitializeComponent() |
| 210 | + Dim textColumn As GridViewTextBoxColumn = New GridViewTextBoxColumn("Description") |
| 211 | + Me.radGridView1.Columns.Add(textColumn) |
| 212 | + textColumn.WrapText = True |
| 213 | + Me.radGridView1.AutoSizeRows = True |
| 214 | + Me.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill |
| 215 | + Me.radGridView1.BeginUpdate() |
| 216 | + |
| 217 | + For i As Integer = 0 To 10 - 1 |
| 218 | + Me.RadGridView1.Rows.Add(i & "0.Data" & Environment.NewLine & i & "1.Data") |
| 219 | + Next |
| 220 | + |
| 221 | + Me.radGridView1.EndUpdate() |
| 222 | + AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf radGridView1_CellEditorInitialized |
| 223 | + AddHandler Me.RadGridView1.ValueChanging, AddressOf radGridView1_ValueChanging |
| 224 | +End Sub |
| 225 | + |
| 226 | +Private Sub radGridView1_ValueChanging(ByVal sender As Object, ByVal e As ValueChangingEventArgs) |
| 227 | + Me.radGridView1.CurrentCell.Value = e.NewValue |
| 228 | +End Sub |
| 229 | + |
| 230 | +Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs) |
| 231 | + Dim tbEditor As RadTextBoxEditor = TryCast(e.ActiveEditor, RadTextBoxEditor) |
| 232 | + |
| 233 | + If tbEditor IsNot Nothing Then |
| 234 | + tbEditor.Multiline = True |
| 235 | + tbEditor.AcceptsReturn = True |
| 236 | + End If |
| 237 | +End Sub |
| 238 | + |
| 239 | +```` |
| 240 | + |
| 241 | +# See Also |
| 242 | + |
| 243 | +* [Resizing rows]({%slug winforms/gridview/rows/resizing-rows%}) |
| 244 | + |
0 commit comments