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

Validation at GridBoundColumn

5 Answers 311 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rahul Barpha
Top achievements
Rank 1
Rahul Barpha asked on 07 May 2010, 09:29 AM

 

hi
i have a standard grid with a series of columns in it i.e GridDropDownColumn, GridBoundColumn etc.
i want to know if there is a way of making the column a required field and validate it as such.
i know you can do it with a template column , but i dont want to have to create template columns for all the fields that require validation , there must be a simpler way to make a column a required field.
just to clarify this is for the insert/edit modes of the grid i need to validate the fields for.

thanks for your input on this.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 07 May 2010, 11:27 AM
Hello Rahul,

You can attach the ItemCreated event of the grid to add a validator to the TextBox editor of GridBoundColumn. The following documentation describes more on this.
Validation

Regards,
Princy.
0
Rahul Barpha
Top achievements
Rank 1
answered on 09 May 2010, 07:16 AM
Thanks Princy,

But it's not working :( ,thay give error

Control 'CommunityNameID' referenced by the ControlToValidate property of '' cannot be validated.


My C# Coad is :

if

 

(e.Item is GridEditableItem && e.Item.IsInEditMode)

 

{

 

GridEditableItem item = e.Item as GridEditableItem;

 

 

DropDownList dropDownList1 = item["CommunityNameID"].Controls[0] as DropDownList;

 

 

GridDropDownColumnEditor editor = (GridDropDownColumnEditor)item.EditManager.GetColumnEditor("CommunityNameID");

 

 

TableCell cell = (TableCell)editor.ContainerControl;

 

 

RequiredFieldValidator validator = new RequiredFieldValidator();

 

editor.ContainerControl.ID =

"CommunityNameID";

 

validator.ControlToValidate = editor.ContainerControl.ID;

validator.InitialValue =

"--Select--";

 

validator.ErrorMessage =

"*";

 

cell.Controls.Add(validator);

}


And HTML Coad is

 

<telerik:GridDropDownColumn DataField="CommunityNameID" HeaderText="CommunityNameID"

 

 

UniqueName="CommunityNameID" HeaderStyle-Width="80px" DataSourceID="SqlDataSource2"

 

 

ListTextField="CommunityName" ListValueField="CommunityNameID" ColumnEditorID="CommunityNameID"

 

 

DropDownControlType="RadComboBox">

 

 

<ItemStyle HorizontalAlign="Center" Wrap="false" />

 

 

</telerik:GridDropDownColumn>

Plz Give me some idea for remove this error :)

 

0
Radoslav
Telerik team
answered on 12 May 2010, 01:59 PM
Hello Rahul,

To archive the desired functionality you need to use the following code snippet:
void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
{
   if (e.Item is GridEditableItem && e.Item.IsInEditMode)
   {
      GridEditableItem item = e.Item as GridEditableItem;
TableCell cell =(TableCell)(((Telerik.Web.UI.GridDropDownListColumnEditor)(editor))).DropDownListControl.Parent;    
       //Validate RadComboBox
       RadComboBox comboBox = item["CommunityNameID"].Controls[0] as RadComboBox;
       GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)item.EditManager.GetColumnEditor("CommunityNameID");
       RequiredFieldValidator validator = new RequiredFieldValidator();
       editor.ContainerControl.ID = "CommunityNameID";
       validator1.ControlToValidate = comboBox.ID;
       validator1.InitialValue = "--Select--";
       validator1.ErrorMessage = "*";
       cell.Controls.Add(validator); 
     }
}

Additionally I am sending you a simple example which demonstrates the desired functionality. Please check it out and let me know if it helps you.

Sincerely yours,
Radoslav
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
Rory
Top achievements
Rank 1
answered on 14 Aug 2012, 09:28 AM
So does that mean there's no way to declaratively add validation to a GridBoundColumn? And therefore the options are: 
1) use GridTemplateColumn instead, where you specify the ItemTemplate and EditItemTemplate, OR
2) use the programmatic solution above to create field validators and hook them up to the GridBoundColumn items
please confirm. 

thanks, 

Rory
0
Radoslav
Telerik team
answered on 15 Aug 2012, 05:45 AM
Hi Rory,

Indeed the described approaches are only way to have validation into the grid’s column. Unfortunately the RadGrid does not support declaratively adding a validation to the GridBoundColumn.

I hope this helps.

Regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Rahul Barpha
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rahul Barpha
Top achievements
Rank 1
Radoslav
Telerik team
Rory
Top achievements
Rank 1
Share this question
or