Line return on text cell

Hi :

When to create the sfdatagrid allow user to edit. How to make the cell can multi line edit. (E.g. Alt-Enter / Shift Enter will line return). (The default action of return key will auto add row).
2. how to allow user to adjust row height (just like use mouse to adjust) ?
3. How to focus on the first column on after row added.


Thank you 

Mike

5 Replies

MA Mohanram Anbukkarasu Syncfusion Team February 17, 2020 02:13 PM UTC

Hi Michael, 

Thanks for contacting Syncfusion support. 

Query 
Solution 
How to make the cell can multi line edit. (E.g. Alt-Enter / Shift Enter will line return). (The default action of return key will auto add row). 
You can achieve this by creating  a custom renderer for the text column and enable the Multiline for the UI element as given in the following code example. 

Code example :  

public Form1() 
{ 
    InitializeComponent(); 
    sfDataGrid1.DataSource = new OrderInfoCollection().OrdersListDetails; 
    sfDataGrid1.CellRenderers["TextBox"] = new CustomTextBoxCellRenderer(this.sfDataGrid1); 
} 
 
 
public class CustomTextBoxCellRenderer : GridTextBoxCellRenderer 
{ 
    SfDataGrid DataGrid; 
    public CustomTextBoxCellRenderer(SfDataGrid DataGrid) 
    { 
        this.DataGrid = DataGrid; 
    } 
    protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, TextBox uiElement) 
    { 
        base.OnInitializeEditElement(column, rowColumnIndex, uiElement); 
        uiElement.Multiline = true; 
        uiElement.ScrollBars = ScrollBars.Vertical; 
    } 
 
    protected override void OnKeyDown(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, KeyEventArgs e) 
    { 
        if (e.KeyCode == Keys.Enter && this.DataGrid.CurrentCell.IsEditing) 
            return; 
        base.OnKeyDown(dataColumn, rowColumnIndex, e); 
    } 
} 


how to allow user to adjust row height (just like use mouse to adjust) ? 
You can change the row height for the default rows and the header rows as given in the following code example.  

Code example :  

//Set the row height for the default row. 
this.sfDataGrid.RowHeight = 40; 
 
//Set the row height for header row 
this.sfDataGrid.HeaderRowHeight = 70; 


Please let us know whether your requirement is to change the row height by resizing the rows using mouse.  

How to focus on the first column on after row added. 
This can be achieved by moving the current cell to the desired cell on the DataGrid.View.Records.CollectionChanged event as shown in the following code example.  

Code example :  

this.sfDataGrid1.View.Records.CollectionChanged += Records_CollectionChanged; 
 
private void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
    if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) 
    { 
        this.sfDataGrid1.BeginInvoke(new Action(() => 
       { 
           this.sfDataGrid1.MoveToCurrentCell(new RowColumnIndex(this.sfDataGrid1.TableControl.ResolveToRowIndex(e.NewItems[0]), 0)); 
       })); 
    } 
} 





Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 



MK Michael K February 17, 2020 04:07 PM UTC

Dear Mohanram :

Thanks for your advise. About you provide example one 
sfDataGrid1.CellRenderers["TextBox"] = new CustomTextBoxCellRenderer(this.sfDataGrid1); 

It seem will change all textbox field allow multiline. Can you provide another example which is specify columns allow multi line. Let say
sfDataGrid1.Columns[2] = new CustomTextBoxCellRenderer(this.sfDataGrid1);  // When Column is the textbox column allow multu line , for other is not multi line.

For the example 2. Row height is control by programming code, do sfgrid have properties that allow all row is re-sizable. Just like excel allow user adjust row height by themselves ?

Thank you 


SS Susmitha Sundar Syncfusion Team February 18, 2020 06:49 PM UTC

Hi Michael, 
 
Thank you for your update. 
 
Please find the details for your query, 
 
You can use the TextBox.MultiLine property for specified column by check the condition for Column.MappingName property. Refer the below code, 
 
C#: 
public class CustomTextBoxCellRenderer : GridTextBoxCellRenderer 
 { 
     SfDataGrid DataGrid; 
     public CustomTextBoxCellRenderer(SfDataGrid DataGrid) 
     { 
         this.DataGrid = DataGrid; 
     } 
     protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, TextBox uiElement) 
     { 
         base.OnInitializeEditElement(column, rowColumnIndex, uiElement); 
         if(column.GridColumn.MappingName == "ProductName") 
         { 
             uiElement.Multiline = true; 
             uiElement.ScrollBars = ScrollBars.Vertical; 
         } 
         
     } 
 
     protected override void OnKeyDown(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, KeyEventArgs e) 
     { 
         if (dataColumn.GridColumn.MappingName == "ProductName") 
         { 
             if (e.KeyCode == Keys.Enter && this.DataGrid.CurrentCell.IsEditing) 
                 return; 
         } 
         base.OnKeyDown(dataColumn, rowColumnIndex, e); 
     } 
 } 
 
Query 2: Row height is control by programming code, do sfgrid have properties that allow all row is re-sizable. Just like excel allow user adjust row height by themselves ? 
 
Currently, we did not have resizing support for SfDataGrid row. But we have already logged the feature request for this. We will provide the feedback link on February 19, 2020. 
 
 
We appreciate your patience until then. 
 
Regards, 
Susmitha S 



MK Michael K February 19, 2020 09:54 AM UTC

Dear Susmitha, Mohanram   :

Thank you , you can help me 
:)




SS Susmitha Sundar Syncfusion Team February 19, 2020 10:46 AM UTC

Hi Michael, 
 
Thank you for your update. 

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 

Query 2: Row height is control by programming code, do sfgrid have properties that allow all row is re-sizable. Just like excel allow user adjust row height by themselves ?  
 
 
At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We will let you know when this feature is implemented. We appreciate your patience until then. 
 
You can also communicate with us regarding the open features any time using our Feature Report page.  
  
  
 
Regards, 
Susmitha S 
 


Loader.
Up arrow icon