I have a grid with combobox column. I would like to get the selected item in the combo box for use in my CellValidating event, and cast that into an object.
I suspect I need something like this:
private
void
myGrid_CellValidating(
object
sender, GridViewCellValidatingEventArgs e)
{
fooEntity selectedEntity = e.Row.Column[1].CurrentItem
as
fooEntity;
//How to do this??
if
(e.Cell.Column.UniqueName ==
"validateThisColumn"
)
{
if
(e.NewValue.ToString()== selectedEntity.SomeProperty)
{
e.IsValid =
false
;
e.ErrorMessage =
"No way!"
;
}
}
}
However, I cannot figure out the syntax to get the item from the combox and cast it to my entity.
Thanks!
5 Answers, 1 is accepted
Basically, during CellValidating event you can get the current item from the grid and it would be of the same type (you will find the selected item from the source of the grid). Getting the item from the source of GridViewComboBoxColumn will require some additional logic - to get the property for the corresponding column and find the corresponding item from the source of the column.
Could you clarify which item exactly do you want to get - the one from the grid or the one from the column ?
Maya
the Telerik team
Thanks for your prompt response.
I would like to get the current item in the combobox, in the RowValidating event.
I saw the extension methods, but can't get an instance of the combobox:
private
void
ProjectMetricChildGrid_RowValidating(
object
sender, GridViewRowValidatingEventArgs e)
{
GridViewRow validatingRow = e.Row;
RadComboBox projectSubjectCmb = validatingRow.ChildrenOfType<RadComboBox>().FirstOrDefault();
//projectSubjectCmb is always null
fooEntity selectedFoo = projectSubjectCmb.SelectedItem
as
fooEntity;
//my validation logic continues.....
}
-Curt
Actually, that would be the expected behavior since during RowValidating event the row is getting out of edit mode and the editing element of GridViewComboBoxColumn (RadComboBox) is hidden. What you can try is to find the element in the source of the column that corresponds to the property from the source of the grid. I am attaching a sample project so that you can test the suggested approach. Could you take a look at it and let me know whether it corresponds to your requirements ?
Maya
the Telerik team
I marked your post as an answer, and I followed the pattern in your sample (get the ID from the combo, then query the Entity to get a copy of the selected item in the combo).
I am happy to see that the suggested approach fits into your requirements. Let me know in case you need further assistance.
Maya
the Telerik team