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
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.
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 :)
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.
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
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