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

Auto row-height with multiline cell

5 Answers 2054 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sascha
Top achievements
Rank 1
Sascha asked on 04 Dec 2019, 04:55 PM

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

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Dec 2019, 08:20 AM
Hello, Sascha, 

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sascha
Top achievements
Rank 1
answered on 09 Dec 2019, 02:38 PM

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

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Dec 2019, 09:27 AM
Hello, Sascha, 

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sascha
Top achievements
Rank 1
answered on 11 Dec 2019, 12:29 PM

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

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Dec 2019, 01:36 PM
Hello, Sascha, 

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
TestTeam
Top achievements
Rank 2
Iron
commented on 18 Apr 2023, 04:04 PM

Good evening Dess,

I would like  to resize row height when adding new row when is a GridViewNEWRowInfo.

Thanks,

Carlo

Dess | Tech Support Engineer, Principal
Telerik team
commented on 20 Apr 2023, 12:06 PM

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 

TestTeam
Top achievements
Rank 2
Iron
commented on 09 May 2023, 06:26 AM

Thank you Dess, but when I insert text in a new row the height of the row does not change size.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 09 May 2023, 11:24 AM

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.

Tags
GridView
Asked by
Sascha
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Sascha
Top achievements
Rank 1
Share this question
or