Articles in this section
Category / Section

How to place the cursor while editing in GridNumericColumn in WinForms DataGrid?

3 mins read

WinForms DataGrid (SfDataGrid) enters into editing in GridNumericColumn places the cursor at the last of the edit element. You can places the cursor based on the entered number in a cell by overriding the OnInitializeEditElement method in GridNumericCellRenderer.

this.sfDataGrid.CellRenderers.Remove("Numeric");
this.sfDataGrid.CellRenderers.Add("Numeric", new GridNumericCellRendererExt(sfDataGrid));
 
public class GridNumericCellRendererExt : GridNumericCellRenderer
{
    private SfDataGrid dataGrid;

    public GridNumericCellRendererExt(SfDataGrid dataGrid) : base()
    {
        this.dataGrid = dataGrid;
    }

    protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, SfNumericTextBox uiElement)
    {
        base.OnInitializeEditElement(column, rowColumnIndex, uiElement);
        // Below code is used to set caret position based on mouse position
        switch (dataGrid.EditorSelectionBehavior)
        {
            case EditorSelectionBehavior.Default:
                uiElement.SelectionStart = 1;
                uiElement.SelectionLength = 0;
                break;
            case EditorSelectionBehavior.SelectAll:
                uiElement.SelectAll();
                break;
            case EditorSelectionBehavior.MoveLast:
                uiElement.SelectionStart = uiElement.Text.Length;
                uiElement.SelectionLength = 0;
                break;
        }
    }
}

Shows the cursor properly places while editing in GridNumericColumn


Take a moment to peruse the WinForms DataGrid - Editing documentation, where you can find about editing with code examples.

View sample in GitHub.

Conclusion

I hope you enjoyed learning about how to place the cursor while editing in GridNumericColumn in DataGrid.

You can refer to our WinForms DataGrid  feature tour page to know about its other groundbreaking feature representations, WinForms DataGrid documentation and how to quickly get started for configuration specifications. You can also explore our WinForms DataGrid  example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied