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.
Unfortunately, activation email could not send to your email. Please try again.

Preventing unwanted character input in NumericUpDown cells

Thread ID:

Created:

Updated:

Platform:

Replies:

81397 May 19,2009 05:38 PM May 20,2009 08:59 AM Windows Forms 1
loading
Tags: GridControl
Brian R
Asked On May 19, 2009 05:38 PM

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

Rajadurai C [Syncfusion]
Replied On May 20, 2009 08:59 AM

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;