This is a migrated thread and some comments may be shown as answers.

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

7 Answers 1724 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Shamjith
Top achievements
Rank 1
Shamjith asked on 23 Dec 2008, 09:42 AM
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?

7 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 23 Dec 2008, 05:00 PM
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,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
DEWISME Vincent
Top achievements
Rank 1
answered on 19 May 2010, 04:14 PM
Hello,

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.

Thanks

Bye


0
Jack
Telerik team
answered on 20 May 2010, 05:18 PM
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,
Jack
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.
0
Usman
Top achievements
Rank 2
answered on 12 Jun 2014, 11:16 AM
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
Thanks.!!
0
Anton
Telerik team
answered on 17 Jun 2014, 09:42 AM
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.

Regards,
Anton
Telerik
 
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.
 
0
Karl B
Top achievements
Rank 1
answered on 01 Dec 2015, 05:00 PM
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?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Dec 2015, 09:28 AM
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()
{
    InitializeComponent();
    GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn();
    radGridView1.MasterTemplate.Columns.Add(decimalColumn);
 
    for (int i = 0; i < 10; i++)
    {
        this.radGridView1.Rows.Add(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;
        el.TextBoxItem.HostedControl.KeyPress+=HostedControl_KeyPress;
    }
}
 
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.
 
Regards,
Dess
Telerik
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
Tags
GridView
Asked by
Shamjith
Top achievements
Rank 1
Answers by
Jack
Telerik team
DEWISME Vincent
Top achievements
Rank 1
Usman
Top achievements
Rank 2
Anton
Telerik team
Karl B
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or