RadVirtualGrid Edit Cell Multi-line

1 Answer 200 Views
VirtualGrid
JinSub
Top achievements
Rank 1
JinSub asked on 09 Aug 2021, 03:02 AM

How to use multiple lines when editing cells?

https://www.telerik.com/forums/auto-row-height-with-multiline-cell

Although I refer to the above page, cells with multiple lines work normally, but cells with only one line do not display text in Edit-Mode.

 

        private void gridTextData__CellEditorInitialized(object sender, VirtualGridCellEditorInitializedEventArgs e)
        {
            var textediter = e.ActiveEditor as VirtualGridTextBoxEditor;
            if (textediter != null)
            {
                textediter.Multiline = true;
            }

        }

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Aug 2021, 06:05 AM
Hello, JinSub,

The referred forum post seems to be relevant for RadGridView. However, the specified product in this thread is RadVirtualGrid. Please have in mind that both controls have different internal implementation and a code snippet that is applicable to RadGridView may not be relevant for RadVirtualGrid.

As to the multiline text in edit mode, indeed, handling the CellEditorInitialized event and setting the VirtualGridTextBoxEditor.Multiline property to true is the correct approach for making the editor multiline. However, in order to insert a new line while the editor is active, feel free to press Ctrl+Enter. Then, in order to see the multiple text lines, the row's height should be adjusted and increased accordingly. 

I have prepared a sample code snippet for your reference which result is illustrated in the attached gif file. Please give the attached project a try and see how it works on your end. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best: 
        DataTable dt = new DataTable();
        public RadForm1()
        {
            InitializeComponent();

            dt.Columns.Add("Description");
            for (int i = 0; i < 20; i++)
            {
                dt.Rows.Add("Data"+i);
            }
            this.radVirtualGrid1.AutoSizeColumnsMode = VirtualGridAutoSizeColumnsMode.Fill; 

            this.radVirtualGrid1.ColumnCount = dt.Columns.Count;
            this.radVirtualGrid1.RowCount = dt.Rows.Count;
            this.radVirtualGrid1.CellValueNeeded += radVirtualGrid1_CellValueNeeded;
            this.radVirtualGrid1.CellValuePushed+=radVirtualGrid1_CellValuePushed;
            this.radVirtualGrid1.CellEditorInitialized += radVirtualGrid1_CellEditorInitialized;
            this.radVirtualGrid1.ValueChanged+=radVirtualGrid1_ValueChanged;
        }

        private void radVirtualGrid1_ValueChanged(object sender, EventArgs e)
        {
            var textEditor = this.radVirtualGrid1.ActiveEditor as VirtualGridTextBoxEditor;
            if (textEditor != null && rowIndex>-1)
            {
                RadTextBoxEditorElement tb = textEditor.EditorElement as RadTextBoxEditorElement;
                this.radVirtualGrid1.MasterViewInfo.SetRowHeight(rowIndex, tb.TextBoxItem.Lines.Length * 20); 
            }
        }

        //https://docs.telerik.com/devtools/winforms/controls/virtualgrid/working-with-data/handle-add-delete-update-of-rows

        private void radVirtualGrid1_CellValuePushed(object sender, VirtualGridCellValuePushedEventArgs e)
        {
            dt.Rows[e.RowIndex][e.ColumnIndex] = e.Value;
            rowIndex = -1;
        }

        int rowIndex = -1;
        private void radVirtualGrid1_CellEditorInitialized(object sender, VirtualGridCellEditorInitializedEventArgs e)
        {
            var textEditor = e.ActiveEditor as VirtualGridTextBoxEditor;
            if (textEditor != null)
            {
                textEditor.Multiline = true;
                rowIndex = e.RowIndex;
            }
        }

        private void radVirtualGrid1_CellValueNeeded(object sender, VirtualGridCellValueNeededEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 )
            {
                e.Value = dt.Rows[e.RowIndex][e.ColumnIndex];
            }
            else
            {
                e.Value = "Column";
            }
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

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