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

[Solved] Radgrid Editmode validation with AutoGenerateColumns= true

2 Answers 230 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dimitri Troncquo
Top achievements
Rank 1
Dimitri Troncquo asked on 13 Oct 2009, 01:07 PM
Hi,

I have a RadGrid with AutoGenerateColumns= true. I tried adding a RequiredFieldValidator as described in http://www.telerik.com/help/aspnet/grid/grdvalidation.html
I hooked up the validator in the ItemCreated event and this works fine (the validator performs as expected). The problem however, arises when the update button is pressed. 

When in try to call:
item.OwnerTableView.ExtractValuesFromItem(newValues, item);  // item is GridEditableItem 
the returned values for the items with a validator attached are always null. The values for items without a validator are returned correctly.

 I suspect the problem arises from the AutoGenerateColumns= true (or the documentation on the subject is just plain wrong, which i suspect is not the case). Is there a workaround for this issue? 

I'm currently using Telerik.Web.UI.dll version 2009.1.311.35

2 Answers, 1 is accepted

Sort by
0
Dimitri Troncquo
Top achievements
Rank 1
answered on 13 Oct 2009, 03:58 PM
I just updated my Telerik controls to the latest version, hoping this would resolve the issue, however, this only seems to have made matters worse, as now the RequiredFieldValidator does not function properly anymore. This is my code for the ItemCreated event:

        protected void grid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) { 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode) { 
                GridEditableItem item = e.Item as GridEditableItem; 
                foreach (string requiredField in this.RequiredFields) { 
                    IGridColumnEditor editor = null
                    try { 
                        editor = item.EditManager.GetColumnEditor(requiredField); 
                    } catch { } 
                    if (editor != null && editor.ContainerControl != null && editor.ContainerControl.Controls.Count >= 1) { 
                        RequiredFieldValidator validator = new RequiredFieldValidator(); 
                        validator.ControlToValidate = editor.ContainerControl.Controls[0].ID = requiredField; 
                        validator.ErrorMessage = "*"
                        editor.ContainerControl.Controls.Add(validator); 
                    } 
                } 
            } 
        } 

0
Sebastian
Telerik team
answered on 16 Oct 2009, 02:39 PM
Hello Danny,

Indeed when you add validators to grid cells dynamically the methods from the server-side API which extract automatically data from the edited cells will not function properly. This is due to the fact that these methods rely on the Controls collection of the cells.

In this case your option is to fetch the new values through the respective column editors as illustrated on this online demo of RadGrid:

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editmodes/defaultcs.aspx

I hope this is feasible workaround for you.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Dimitri Troncquo
Top achievements
Rank 1
Answers by
Dimitri Troncquo
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or