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

validation example on data edit/insert

3 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vin
Top achievements
Rank 1
Vin asked on 18 Nov 2010, 10:14 PM
I have a sqldatasource databound radgrid i'm using for data entry.

date, start mileage, end mileage, personal miles

prior to update/insert , i need to check to ensure end mileage > start mileage, and personal miles is not > (endmileage - startmileage)

I've fudged around on the documentation site but cannot find any concrete examples I can adapt to accomplish this.

anyone have suggestions?

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Nov 2010, 05:52 AM
Hello Vin,


There is a documentation which shows ho to add validator control to edit from in grid. I hope this would  help you in achieving the required.
Validation


-Shinu.
0
Vin
Top achievements
Rank 1
answered on 19 Nov 2010, 04:13 PM
Thanks for your reply... unfortunately I really don't understand the example at all, it appears to add a required field validator to an input field, and I actually need much more complex field validation than that -- fields relating to one another, must be greater than the last record input, etc.

Ideally what I was hoping for was an example of how, when the user clicks the update button on the edit form, I can extract the edited values from the edit form prior to the update/insert operation, perform my consistency checks programmatically, and then cancel the update/insert if there is an error to address.

I was able to get some basic validation by customizing the following code 

 

protected void RadGrid_Mileage_ItemCommand(object sender, GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case RadGrid.EditCommandName:
                try
                {
                    GridDataItem item = (GridDataItem)e.Item;
                    foreach (GridDataItem dataItem in RadGrid_Mileage.Items)
                        if (dataItem.IsInEditMode && dataItem.ItemIndex != item.ItemIndex)
                            e.Canceled = true;
                    if (!e.Canceled)
                        SqlDataSource_Vehicles.FilterExpression = "Employee_GUID = '" + Session["Employee_GUID"].ToString() + "'";
                }
                catch { e.Canceled = true; }
                break;
            case RadGrid.InitInsertCommandName:
                try
                {
                    //GridDataInsertItem item = (GridDataInsertItem)e.Item;
                    foreach (GridDataItem dataItem in RadGrid_Mileage.Items)
                        if (dataItem.IsInEditMode)
                            e.Canceled = true;
                    if (!e.Canceled)
                        SqlDataSource_Vehicles.FilterExpression = "Employee_GUID = '" + Session["Employee_GUID"].ToString() + "'";
                }
                catch { e.Canceled = true;  }
                break;
            case RadGrid.UpdateCommandName:
                try
                {
                    GridDataItem item = (GridDataItem)e.Item;
                    System.Collections.Hashtable values = new System.Collections.Hashtable();
                    item.ExtractValues(values);
                    if (!ValidateMileageData(values["ExpenseMileage_GUID"].ToString(), values["Vehicle_Guid"].ToString(), Convert.ToInt32(values["BeginningMileage"]), Convert.ToInt32(values["EndingMileage"]), Convert.ToInt32(values["PersonalMileage"])))
                        e.Canceled = true;
                }
                catch { e.Canceled = true;  }
                break;
            default:
                SqlDataSource_Vehicles.FilterExpression = "";
                break;
        }
    }
  
    protected Boolean ValidateMileageData(String RecordGUID, String Vehicle_GUID, Int32 StartingMileage, Int32 EndingMileage, Int32 PersonalMileage)
    {
        String gridMessage = String.Empty;
        Object LastEnding = Library.Function.RunSQLCommand("SELECT ISNULL(MAX(EndingMileage), 0) FROM ExpenseMileage WHERE Vehicle_GUID = '" + Vehicle_GUID + "' AND ExpenseMileage_GUID <> '" + RecordGUID + "'");
  
        if (EndingMileage > StartingMileage)
            if (PersonalMileage < (EndingMileage - StartingMileage))
                if (StartingMileage > Convert.ToInt32(LastEnding))
                {
                    RadMaskedTextBox_Mileage.Visible = false;
                    return true;
                }
                else
                    gridMessage = "Starting Mileage must be > " + StartingMileage.ToString();
            else
                gridMessage = "Personal miles in excess of total miles driven.";
        else
            gridMessage = "Ending mileage must be greater than beginning miles";
  
        return false;
    }

This seems to intercept the appropriate error conditions for me... and will cancel the insert/update operation.

What I'm trying to figure out now is how to get the grid message to display since the examples I've seen here place it in the radgrid_databound event, and this event does not fire again (nor do I really want to re-fire it manually as I do not want to replace the user's typed values w/ the original database values)... Looking for some type of radgrid.refresh event now...

 

0
Radoslav
Telerik team
answered on 25 Nov 2010, 11:37 AM
Hello Fin,

Could you please elaborate a bit more on your scenario. Where do you want to show the gridMessage? Next to the a control into the edit/insert form or somewhere on the page (above or under the RadGrid)?
Also you could check out the following example which shows a similar message when an exception is occurred or the CRUD operation is succeeded:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx

Looking forward for your reply.

Regards,
Radoslav
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Vin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Vin
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or