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.,
0or0.00), allow only numeric input. -
If the cell format is text (
@), automatically convert to uppercase as typing.
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.
Raj Clinton G
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 _); } } |
Please let us know if you need any further assistance or clarification.
Raj Clinton G
Attachment: SampleAndVideo_e116fedc.zip
Hello Raj,
Your example project works as expected. Thank you very much for your extraordinary support.
Regards,
Vadim
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
- 4 Replies
- 2 Participants
-
VA Vadim
- Jan 6, 2026 07:10 PM UTC
- Jan 12, 2026 05:32 AM UTC