Setting text position within a single cell and wrap text

Hi

I'm using a winforms sfDatagrid where I have merged some cells using QueryCoveredRange. I have also set text position for a cell using QueryCellStyle like below and all columns have AllowTextWrapping = true

private static void _sfdg_QueryCellStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryCellStyleEventArgs e)

{

    if (e.RowIndex == 4 || e.RowIndex == 6)

    {

        e.Style.HorizontalAlignment = HorizontalAlignment.Left;

        e.Style.VerticalAlignment = VerticalAlignment.Top;

    }

    else

    {

        e.Style.HorizontalAlignment = HorizontalAlignment.Center;

        e.Style.VerticalAlignment = VerticalAlignment.Center;

    }

}


There are two things that I would like to get working.

   1. When I start editing a cell where the text is centered, the text jumps to top left. Is it possible to have the text centered when it's edited?

   2. When I edit a cell where the text is wrapped, the text is no longer wrapped in edit mode. Is it possible to keep the text wrapped in edit mode?


I have looked at CurrentCellBeginEdit but I have not been able to get it working.



Regards

Øyvind Knappskog Olsen


1 Reply

SB Sweatha Bharathi Syncfusion Team February 26, 2025 01:43 PM UTC

Hi  Øyvind Knappskog Olsen.


We have reviewed your query. When using QueryCellStyle to align the text position, it will reflect in the DataGrid UI. However, when you try to edit the cell, the editor control will be loaded. Therefore, you need to set the alignment for the editor control as well. For a NumericColumn, we use SfNumericTextBox, and for a TextColumn, we use TextBox as the editor control. You will also need to set the text alignment and enable multiline (Multiline = true) for the TextBox to ensure that both alignment and text wrapping are reflected while editing.
We have an event that triggers when the editor control is loaded. In this event, you can set the alignment for the text displayed while editing the current cell. Additionally, you may need to set the style for a particular row and column index by accessing e.RowIndex or e.ColumnIndex. You can access the editor control using e.Control, and based on your needs, you can use this event to set the style of the editor control. Please refer to the UG (User Guide) for further reference.

Code Snippet :
 private void SfDataGrid1_EditingControlShowing(object sender, Syncfusion.WinForms.DataGrid.Events.DataGridEditingControlShowingEventArgs e)
 {
     if (e.RowIndex == 4)
     {
         if (e.Control is TextBox)
         {
             (e.Control as TextBox).TextAlign = HorizontalAlignment.Center;
             (e.Control as TextBox).Multiline = true;
         }
     }
 
   
 }




Regards,
Sweatha.B

Loader.
Up arrow icon