How to restrict user from setting negative values to editable decimal column.

8 posts, 0 answers
  1. Shamjith
    Shamjith avatar
    15 posts
    Member since:
    Sep 2008

    Posted 23 Dec 2008 Link to this post

    I have a gridview with one text column and five decimal columns. These decimal columns are editable. So inorder to edit this, the user can directly enter values or use the up and down buttons to increase or decrease values. My requirement is to restrict user from setting negative values by pressing the down button of the decimal column. How to achieve this?
  2. Jack
    Jack avatar
    2335 posts

    Posted 23 Dec 2008 Link to this post

    Hello Shamjith,

    You can restrict the spin editor when processing the EditorRequired event in RadGridView. Consider the following code snippet:

    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e) 
        GridSpinEditor editor = e.Editor as GridSpinEditor; 
        if (editor != null
            editor.MinValue = 0; 

    If you have any other questions, I will be glad to answer them.

    All the best,
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. DEWISME Vincent
    DEWISME Vincent avatar
    4 posts
    Member since:
    May 2010

    Posted 19 May 2010 Link to this post


    It seems to me that the solution you give does not work.

    Using the debugger, I can see that the Editor property of the event argument gives null.

    I found a way to achieve setting the min and max of the GridSpinEditor using the ActiveEditor property of the RadGridView instance :

                GridSpinEditor editor = this.radGridView1.ActiveEditor as GridSpinEditor; 
                if (editor != null) 
                    editor.MinValue = Decimal.Zero; 
                    editor.MaxValue = 1000m

    I think this could be helpful.



  5. Jack
    Jack avatar
    2335 posts

    Posted 20 May 2010 Link to this post

    Hi DEWISME Vincent,

    In 2009 we changed the behavior of EditorRequired event. You should handle this event when you want to replace the default editor with a custom one.

    In order to customize editor properties you have to handle CellBeginEdit or CellEditorInitialized events. More details are available in our online help.

    Currently you don't need to handle an event at all. You can set the Minimum and Maximum properties of GridViewDecimalColumn. Here is a sample:

    GridViewDecimalColumn decimalColumn = (GridViewDecimalColumn)this.radGridView1.Columns["Value"];
    decimalColumn.Minimum = 5;
    decimalColumn.Maximum = 120;

    I hope this helps.

    Kind regards,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Usman
    Usman avatar
    3 posts
    Member since:
    Sep 2013

    Posted 12 Jun 2014 in reply to Jack Link to this post

    Heloo ... it works for decimal column but i am not able to do the same with a int type column.
    i want to restrict the editable columns to non negative values.
    need help
  7. Anton
    Anton avatar
    167 posts

    Posted 17 Jun 2014 Link to this post

    Hi Usman,

    Thank you for writing.

    RadGridView does not have "int type column", it has GridViewDecimalColumn which can be bound to fields of any numeric type. You can read more about this column and how to use it here.

    To achieve only positive integer numbers, you just need to set the decimal column DataType to int and set the desired minimum:

    GridViewDecimalColumn myDecimalColumn = (GridViewDecimalColumn)radGridView1.Columns["MyDecimalColumn"];
    myDecimalColumn.DataType = typeof(int);
    myDecimalColumn.Minimum = 0;

    However, if you are using some other kind of column like GridViewTextBoxColumn and you want the text to be a non negative numeric value, you can use the ValueChanging event to prevent entering of negative values or the CellValidating event to prevent users to leave the cell until the the value in the cell is not valid.
    You can find more info about validation in this article here.

    I hope you find this information useful.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  8. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 01 Dec 2015 Link to this post

    Setting these values does not prevent typing a negative sign on the keyboard. Is there a way to prevent the minus sign from being entered when using the keyboard?
  9. Dess
    Dess avatar
    1561 posts

    Posted 04 Dec 2015 Link to this post

    Hello Karl,

    Thank you for writing.

    In order to restrict the user to enter '-' sign in the grid editor, you can handle the KeyPress event of the hosted textbox and set the Handled property to true:
    public Form1()
        GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn();
        for (int i = 0; i < 10; i++)
        this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        GridSpinEditor editor = e.ActiveEditor as GridSpinEditor;
        if (editor != null)
            GridSpinEditorElement el = editor.EditorElement as GridSpinEditorElement;
    private void HostedControl_KeyPress(object sender, KeyPressEventArgs e)
        if (e.KeyChar == 45)
            e.Handled = true;

    I hope this information helps. Should you have further questions I would be glad to help.
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner