We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Preventing unwanted character input in NumericUpDown cells

Hey there,

I have a Grouping Grid control where I have a column set as a NumericUpDown. I have set the Minimum and Maximum of the NumericUpDown to the required values and this works great at preventing a user from clicking the buttons and making the value exceed the minimum and maximum limits. Yet a user can still type in what ever they want. This could be a valid number that exceeds these limits or even a non-numeric value.

What I would like to do is simply prevent the user from typing an invalid value. I have tried a number of different ways but nothing seems to be a 100% solution.

Do you have any suggestions?


Thanks!

Brian

1 Reply

RC Rajadurai C Syncfusion Team May 20, 2009 12:59 PM UTC

Hi Brian,

Thanks for your interest in Syncfusion products.

With NumericUpDown celltype in gridgroupingcontrol, you can prevent user from entering any invalid entries into the cell by handling some validations manually. Please refer to the following code snippet, in which TableControlCurrentCellChanged and TableControlCurrentCellKeyPress events are handled to do such validations.

//TableControlCurrentCellChanged event
void gridGroupingControl1_TableControlCurrentCellChanged(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlEventArgs e)
{
GridCurrentCell cc = e.TableControl.CurrentCell;
if (cc.Renderer is GridNumericUpDownCellRenderer)
{
GridNumericUpDownCellRenderer rend = cc.Renderer as GridNumericUpDownCellRenderer;
if (rend != null && rend.ControlText != "")
{
int val = int.Parse(rend.ControlText);
if (val < min || val > max)
{
cc.EndEdit();
rend.ControlText = value.ToString();
this.gridGroupingControl1.TableModel[cc.RowIndex, cc.ColIndex].Text = value.ToString();
}
else
value = int.Parse(rend.ControlText);
}
}
}
//initialization part
int min=0, max=20, value = 0;

//TableControlCurrentCellKeyPress event
void gridGroupingControl1_TableControlCurrentCellKeyPress(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlKeyPressEventArgs e)
{
GridCurrentCell cc = e.TableControl.CurrentCell;
if (cc.Renderer is GridNumericUpDownCellRenderer)
{
GridNumericUpDownCellRenderer rend = cc.Renderer as GridNumericUpDownCellRenderer;
if (!(e.Inner.KeyChar == '1' || e.Inner.KeyChar == '2' || e.Inner.KeyChar == '3' || e.Inner.KeyChar == '4' || e.Inner.KeyChar == '5' || e.Inner.KeyChar == '6' || e.Inner.KeyChar == '7' || e.Inner.KeyChar == '8' || e.Inner.KeyChar == '9' || e.Inner.KeyChar == '0'))
{
e.Inner.Handled = true;
}
}
}

//QueryCellStyleInfo event
void gridGroupingControl1_QueryCellStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs e)
{
if (e.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.Record && e.TableCellIdentity.ColIndex > 0 )
{
e.Style.CellType = "NumericUpDown";
e.Style.HorizontalAlignment = GridHorizontalAlignment.Right;
e.Style.NumericUpDown = new GridNumericUpDownCellInfo(min,max, 5, 1, false);
e.Style.ShowButtons = GridShowButtons.ShowCurrentCell;
}
}


Regards,
Rajadurai

Loader.
Live Chat Icon For mobile
Up arrow icon