SfSpreadsheet control - enforce data input

Hello,

What is the best way in SfSpreadsheet to enforce data input based on the cell format?

  • If the cell format is numeric (e.g., 0 or 0.00), allow only numeric input.

  • If the cell format is text (@), automatically convert to uppercase as typing.

Thank you,
Vadim

4 Replies

RC Raj Clinton Gnanaprakasam Syncfusion Team January 7, 2026 03:18 PM UTC

Hi Vadim

We are currently analyzing the reported scenario. We need some time to validate the scenario. We will provide further updates on or before January 09, 2026.
Regards,
Raj Clinton G


RC Raj Clinton Gnanaprakasam Syncfusion Team January 9, 2026 11:48 AM UTC

Hi Vadim,

We have reviewed your query and implemented a workaround to ensure that numeric cells accept only numeric input, and text cells automatically convert typed content to uppercase by customizing the CellRenderers in the spreadsheet. Please refer to the code snippet below:

public class TextUpperAndNumericOnlyRenderer : SpreadsheetTextBoxCellRenderer

{

    private readonly SfSpreadsheet _spreadsheet;

 

    private bool _isTextFormat;

    private bool _enforceNumeric;

    private string _lastValidNumeric = "";

    private TextBox _ui;

 

    public TextUpperAndNumericOnlyRenderer(SfSpreadsheet spreadsheet)

    {

        _spreadsheet = spreadsheet;

    }

 

    protected override void OnInitializeEditElement(RowColumnIndex rowColumnIndex, TextBox uiElement, SpreadsheetColumn column)

    {

        base.OnInitializeEditElement(rowColumnIndex, uiElement, column);

 

        _ui = uiElement;

 

        var sheet = _spreadsheet?.ActiveSheet;

        IRange cell = sheet?.Range[rowColumnIndex.RowIndex, rowColumnIndex.ColumnIndex];

        string nf = cell?.NumberFormat ?? column?.ExcelRange?.NumberFormat;

 

        // Text (@) => uppercase + fallback

        _isTextFormat = string.Equals(nf, "@", StringComparison.Ordinal);

        uiElement.CharacterCasing = _isTextFormat ? CharacterCasing.Upper : CharacterCasing.Normal;

        if (_isTextFormat)

            uiElement.TextChanged += UiElement_TextChanged_Upper;

 

        // Numeric => only TryParse

        _enforceNumeric = IsNumericFormat(nf);

        if (_enforceNumeric)

        {

            _lastValidNumeric = IsValidNumeric(uiElement.Text) ? uiElement.Text : "";

            uiElement.TextChanged += UiElement_TextChanged_Numeric;

        }

    }

   

    // Uppercase fallback for Text (@)

    private void UiElement_TextChanged_Upper(object sender, TextChangedEventArgs e)

    {

        if (!_isTextFormat) return;

        var tb = (TextBox)sender;

        var upper = tb.Text.ToUpperInvariant();

        if (!string.Equals(tb.Text, upper, StringComparison.Ordinal))

        {

            int s = tb.SelectionStart, l = tb.SelectionLength;

            tb.Text = upper;

            tb.SelectionStart = s;

            tb.SelectionLength = l;

        }

    }

 

    //  numeric check (TryParse only)

    private void UiElement_TextChanged_Numeric(object sender, TextChangedEventArgs e)

    {

        if (!_enforceNumeric) return;

        var tb = (TextBox)sender;

 

        if (IsValidNumeric(tb.Text))

        {

            _lastValidNumeric = tb.Text;

        }

        else

        {

            int s = tb.SelectionStart;

            tb.Text = _lastValidNumeric;

            tb.SelectionStart = Math.Min(s, tb.Text.Length);

            tb.SelectionLength = 0;

        }

    }

 

    private static bool IsNumericFormat(string nf)

    {

        if (string.IsNullOrWhiteSpace(nf)) return false; // General

        if (nf.Contains("@")) return false;               // Text

        return nf.IndexOf('0') >= 0 || nf.IndexOf('#') >= 0;

    }

 

    private static bool IsValidNumeric(string text)

    {

        if (string.IsNullOrEmpty(text)) return true;

        return double.TryParse(text, NumberStyles.Float | NumberStyles.AllowLeadingSign,

                               CultureInfo.CurrentCulture, out _);

    }

}

We have also attached a video and sample for your reference. Could you please review and confirm whether it works on your end?

Please let us know if you need any further assistance or clarification.
Regards,
Raj Clinton G

Attachment: SampleAndVideo_e116fedc.zip


VA Vadim January 9, 2026 02:55 PM UTC

Hello Raj,

Your example project works as expected. Thank you very much for your extraordinary support.


Regards,

Vadim



RC Raj Clinton Gnanaprakasam Syncfusion Team January 12, 2026 05:32 AM UTC

Hi Vadim

We glad that the query was addressed from your side. Please get back to us if you need any further assistance.

Regards,
Raj Clinton G


Loader.
Up arrow icon