Hello,
actually I am testing the RadGridView.
I made a Grid with three columns. One of them as multiline textbox.
So, my problem is to set the row height dynamically with the number of the lines of the multiline cell.
The user should see all the lines of the multiline cell if the cell will be in edit mode.
I have read all the threads about that, but I don‘t find a solution.
Any help from you.
thank you and best regards
5 Answers, 1 is accepted
In order to display multi-line text in RadGridView, it is necessary to set the WrapText property of the column to true and set the RadGridView.AutoSizeRows property to true.
As to the editor, handle the CellEditorInitialized event and enable the RadTextBoxEditor.Multiline property.
public RadForm1()
{
InitializeComponent();
GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Text");
textColumn.WrapText = true;
this.radGridView1.Columns.Add(textColumn);
this.radGridView1.Rows.Add("One" + Environment.NewLine + "Two" + Environment.NewLine + "Three");
this.radGridView1.AutoSizeRows = true;
this.radGridView1.CellEditorInitialized+=radGridView1_CellEditorInitialized;
}
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
RadTextBoxEditor editor = e.ActiveEditor as RadTextBoxEditor;
if (editor!=null)
{
editor.Multiline = true;
}
}
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Hello,
thank you for the example.
It works in C#.
But I have to realize my software in visual-basic.net.
If I write your code in vb it will not work.
So my question: Please can you tell me your code in vb.net so that I can compare it.
Thank you and best regards
I have prepared a sample VB.NET project for your reference. Please refer to the attached zip file. Could you please give it a try and see how it works on your end?
I am looking forward to your reply.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Hello Dess,
it works.
Thank you.
My further Problem is, that I need the following:
1) At runtime all the lines should be visible if I am editing the cell. So I think I have to make an update of the cell to show all the lines everytime!
2) To Generator a new line manually I have to press control+return. Does there exists a method that I only need to press return for a new line?
If that will work I am going to buy the tool.
thanks and regards
By design, the auto-size rows functionality that RadGridView offers, takes into consideration the cell's value when calculating the required height for the row. However, note that while the editor is active and you enter any text, the value is changed only in the active editor. It is not committed to the cell yet. That is why the row's height is not adjusted until you press Enter to commit the editor's value to the cell.
You can handle the RadGridView.ValueChanged event which is fired when the editor's value is changed and update the associated cell. The following code snippet demonstrates how to achieve the illustrated result in the attached gif file:
this.radGridView1.ValueChanged+=radGridView1_ValueChanged;
private void radGridView1_ValueChanged(object sender, EventArgs e)
{
if (this.radGridView1.CurrentRow is GridViewDataRowInfo)
{
this.radGridView1.CurrentCell.Value = this.radGridView1.ActiveEditor.Value;
}
}
As to the question about pressing Control+Enter to insert a new line in the RadTextBoxEditor, note that it hosts internally the standard MS TextBox from where this behavior comes. You can enable the AcceptsReturn property of the hosted text box in order to allow entering new lines only by pressing Enter. However, in this case, pressing Enter won't commit the editor's value to the cell and ends the edit operation. It would be necessary to press the pencil indicator at the beginning of the row to commit the new value:
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
RadTextBoxEditor editor = e.ActiveEditor as RadTextBoxEditor;
if (editor!=null)
{
editor.Multiline = true;
RadTextBoxEditorElement element = editor.EditorElement as RadTextBoxEditorElement;
element.TextBoxItem.AcceptsReturn = true;
}
}
Should you have further questions please let me know.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Good evening Dess,
I would like to resize row height when adding new row when is a GridViewNEWRowInfo.
Thanks,
Carlo
Carlo, I believe that this approach would be applicable for the new row as well:
https://docs.telerik.com/devtools/winforms/knowledge-base/row-autosizing-while-editing
Carlo, If you copy a very long text (without containing Environment.NewLine in the text) and paste it within a cell (no matter if it belongs to the new or data row) , the height is not increased since the Lines collection returns 1. After researching in general programming forums, I have found the following thread which is helpful for measuring the text according to the applied font and available width: https://stackoverflow.com/questions/1204804/word-wrap-for-a-label-in-windows-forms
I have attached my sample project.