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

Validating user input

9 Answers 539 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tooraj
Top achievements
Rank 1
Tooraj asked on 22 Oct 2010, 01:06 PM
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.

9 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 22 Oct 2010, 03:39 PM
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
0
Phi
Top achievements
Rank 1
answered on 03 Nov 2010, 02:09 AM
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.
0
Phi
Top achievements
Rank 1
answered on 03 Nov 2010, 07:22 PM
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
0
Phi
Top achievements
Rank 1
answered on 03 Nov 2010, 08:42 PM
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;
    }
}
0
Alexander
Telerik team
answered on 08 Nov 2010, 04:46 PM
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
0
Sasireka
Top achievements
Rank 1
answered on 12 Dec 2012, 12:23 PM
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
0
Svett
Telerik team
answered on 14 Dec 2012, 01:38 PM
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.
0
gaurav
Top achievements
Rank 1
answered on 31 Mar 2018, 01:09 PM

as i am new to asp.net
i am having an gridview with an TemplateField textbox whose textmode="time"
now i need to save the value of textbox to database on button click

i am using 
TextBox intime = (GridView1.Rows[i].Cells[3].FindControl("txtintime") as TextBox);

this code but at run time it is having an empty string "" in intime.text; can anyone help me out

 

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Apr 2018, 10:44 AM
Hello, Gaurav,   

I would like to note that this forum is related to the Telerik UI for WinForms suite. However, you questions seems to be related to ASP.NET. Feel free to post your inquiries in the relevant forum: https://www.telerik.com/forums

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Tooraj
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Phi
Top achievements
Rank 1
Alexander
Telerik team
Sasireka
Top achievements
Rank 1
Svett
Telerik team
gaurav
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or