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

Validating a subtotal before saving

1 Answer 105 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Philippe
Top achievements
Rank 2
Philippe asked on 31 May 2012, 01:58 PM
Hello

I am using a RadGridview (2012 Q1) and I wanted to know if there was a way to read/validate the group's total after a modification.

Format    Product    Pct
Can    1000    0.2
Can    1001    0.6
Can    1002    0.2
Plas    1003    0.8
Plas    1004    0.2

If a user modifies a percentage, I would like to wait saving to the database until he corrects another product to make sure the total is 100%

I am setting up my Gridview like this:
radGridView1.GroupDescriptors.Add(new GridGroupByExpression("Format Group By Format"));
GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
item1.Add(new GridViewSummaryItem("pct", "Total: {0:F2} ", GridAggregateFunction.Sum));

In another section of my program, I use radGridView1_CellValidating to save data on a modification (if e.OldValue.ToString() != e.Value.ToString()), but in that case, I didn't need any validation. In this scenario, I do need validation.

Is there a way to use the features of the current gridview to edit directly, or would it be better to build a new form with my format isolated, and fully validating on a button press? Or another workaround I haven't considered?

Thanks.
Philippe

1 Answer, 1 is accepted

Sort by
0
Accepted
Svett
Telerik team
answered on 05 Jun 2012, 09:53 AM
Hi Philippe,

I recommend committing the changes to the data base in the CellEndEdit event. In addition, you should validate your data in the CellValidating event. You should use the Evaluate method of RadGridView to achieve that:

private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
{
    if (e.ActiveEditor != null)
    {
        string columName = e.Column.Name;
        GridViewSummaryItem item = this.radGridView1.SummaryRowsBottom[columName][0];
        string expression = item.GetSummaryExpression();
 
        object value = e.Value;
        List<GridViewRowInfo> childRows = new List<GridViewRowInfo>(e.Row.Parent.ChildRows);
        childRows.Remove(e.Row);
 
        GridViewDataRowInfo dataRow = new GridViewDataRowInfo(e.Row.ViewInfo);
        dataRow.Cells[columName].Value = value;
        childRows.Add(dataRow);
 
        object expressionValue = this.radGridView1.Evaluate(expression, childRows);
        double totalValue = Convert.ToDouble(expressionValue);
        e.Cancel = totalValue > 1.0f;
    }
}

I hope this helps.

Kind regards,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Philippe
Top achievements
Rank 2
Answers by
Svett
Telerik team
Share this question
or