Validating user input

8 posts, 1 answers
  1. Tooraj
    Tooraj avatar
    74 posts
    Member since:
    Aug 2010

    Posted 22 Oct 2010 Link to this post

    Hi,
    I have RadGridView object and I want to Validate user input. there are two coulmns. when the user enters a value which is greater than 0 for column1 then if the column2 is greater than 0 an error must be occured and also this is the rule for coulmn2 (that is if coulmn2 value is greater than 0 and also column1's value is greater than 0 the error occurs.)
    the problem is that first of all I must check the value of a cell. but when I check an error occurs and tells that the value is null:
    if(column1.currentrow.column["MyCoulmn"].value!=null) // Runtime Error:  Value is null, use new keyword!

    how can I validate column content without getting this error. also when I try this:
    Convert.ToInt16(column1.currentrow.column["MyColumn"].value.ToString())>0
    FormatException Occurs. While my value is 2 for example, Why such error occurs?
    Thank you, please help me.
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 22 Oct 2010 Link to this post

    Hello Tooraj,

    First, It is not colum1.CurrentRow, it should be radGridView1.CurrentRow,

    Second, please take a look at the following very basic example of CellValidating
    void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
    {
        var currentRow = radGridView1.CurrentRow;
        var cell = e.Row.Cells[e.ColumnIndex];
     
        if (radGridView1.Columns[e.ColumnIndex].Name == "Num1")
        {
            if (e.Value == null || Convert.ToDouble(e.Value) <= 0)
            {
                e.Cancel = true;
                cell.ErrorText = "Error!";
            }
            else
            {
                cell.ErrorText = string.Empty;
            }
        }
        else
        {
            if (e.Value == null || Convert.ToDouble(e.Value) <= 0 || Convert.ToDouble(currentRow.Cells[0].Value) >= Convert.ToDouble(e.Value))
            {
                e.Cancel = true;
                cell.ErrorText = "Error!";
            }
            else
            {
                cell.ErrorText = string.Empty;
            }
        }
    }
     
    You should either use the cell.ErrorText or cancel the operation, i just added them both here for clarity purposes.
    Another way of doing things, as instructed by the documentation is to handle the ValueChanging event and handling validation there.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Phi
    Phi avatar
    47 posts
    Member since:
    Dec 2008

    Posted 02 Nov 2010 Link to this post

    Just want to point out 1 minor thing, You don't need the line below! You can just use e.Row instead!

    var currentRow = radGridView1.CurrentRow;

    The funny thing is: the CellValidatingEventArgs class gives you everything except the cell itself! It just seems strange to me! I think it would be better to replace Value, RowIndex, and ColumnIndex with a cell object which contains all those properties and more.

    Phi

    PS: BTW, if you set e.Cancel = True, cell.ErrorText does not display in the grid. At least none that I could see.
  5. Phi
    Phi avatar
    47 posts
    Member since:
    Dec 2008

    Posted 03 Nov 2010 Link to this post

    I found out that the CellValidating event fired whenever the current row changed, not just only when editing only. There is no need to validate data when there is no user input! So I opt for the more friendly ValueChanging event. Man! Why would the ValueChangingEventArgs class only has only three useful properties (Cancel, NewValue, and OldValue)? Where are all the row, rowindex, column, columnindex, columns, or rows properties??? I fell bad for the ValueChangingEventArgs class!!! It is not the favorite child in the family!!!

    I guess I would have to resort to the gridView.CurrentColumn.Index and the like to get what I need!

    This is the inconsistency issue again ;)

    Phi
  6. Phi
    Phi avatar
    47 posts
    Member since:
    Dec 2008

    Posted 03 Nov 2010 Link to this post

    Opps! Look like the ValueChanging event is triggered while in editing. It fires whenever the user presses a keystroke. This is not needed in my case where I want to validate the new value when user pressed the Enter key. Back to CellValidating once again!

    The CellValidating event fires whenever user clicks on a row, not just in edit mode. I don't know why that is? Maybe because it could host other type of controls? Anyway, I get around it by using a edit flag like below.  When I put a break point in CellValidating, I found that it fired TWICE whenever I clicked on a row.

    Has anybody report this bug yet?

    private void gridView_CellValidating(object sender, CellValidatingEventArgs e)
    {
        if (isInEditMode == false)
            return;
     
        if (e.Value == null && e.OldValue == null)
            return;
     
        if (e.Value.ToString() == e.OldValue.ToString())
            return;
     
        string value = e.Value == null ? string.Empty : e.Value.ToString().Trim();
        string errorMessage = null;

              ...... blah blah blah
     
        if (errorMessage != null)
        {
            Common.Beep();
            //e.Row.Cells[e.ColumnIndex].ErrorText = errorMessage;
            //e.Row.ErrorText = errorMessage;
            SetStatusText("Error " + errorMessage, StatusLevel.Error);
            e.Cancel = true;
        }
    }
  7. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 08 Nov 2010 Link to this post

    Hello Phi,

    Thank you for your questions.

    The CellValidating event arguments do not contain the cell because, it is the current cell of the control. You can access it using:
    GridDataCellElement cell = this.radGridView1.CurrentCell;

    Thank you for your request to include it in the arguments of the event. We will consider adding it in a future release.

    The ErrorText of the cell is displayed when the cell is hovered with the mouse. You can set the ErrorText of the row to indicate that it is not valid.

    You can use the CurrentRow, CurrentColumn and CurrentCell properties of RadGridView when you handle the ValueChanging event.

    I am not able to reproduce the issue where the CellValidating event is fired twice using the lastest version of the RadGridView control (Q2 2010 SP2). Please send us more details concerning this issue. It will help us to investigate it further.

    Best regards,
    Alexander
    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
  8. Sasireka
    Sasireka avatar
    17 posts
    Member since:
    Nov 2012

    Posted 12 Dec 2012 Link to this post

    Hi All,

    I have an issue while validating the cell of rad gridview.

    I have used cell validating event for checking whether the cell is empty or not. In my sceanrio, i have removed already entered text in the cell and press on the "Enter" key. Now the application display the error message as "Enter Name". Now i am click on the other controls like button or checkbox (outside of the gridview),  some of the control events(telerik controls) were fired and some (Non Telerik controls) was not fired.

    Please clarify... How to restirct the other controls(Non Telerik controls) event

    Regards,
    Sasi
  9. Svett
    Admin
    Svett avatar
    728 posts

    Posted 14 Dec 2012 Link to this post

    Hi Sasireka,

    I did not manage to reproduce the issue with the latest release Q3 2012 SP1. Could you share with us a code snippet that demonstrates how you initialized and used RadGridView? What is the version of the controls that you are using?

    Greetings,
    Svett
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top
UI for WinForms is Visual Studio 2017 Ready