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

CellValidation get entity from GridViewComboBoxColumn

5 Answers 80 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Curt
Top achievements
Rank 1
Iron
Curt asked on 15 Feb 2012, 01:20 AM
Hello

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

Sort by
0
Maya
Telerik team
answered on 15 Feb 2012, 08:03 AM
Hello Curt,

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 ? 

All the best,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Curt
Top achievements
Rank 1
Iron
answered on 15 Feb 2012, 05:32 PM
Maya
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


0
Accepted
Maya
Telerik team
answered on 16 Feb 2012, 12:01 PM
Hello 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 ? 

Kind regards,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Curt
Top achievements
Rank 1
Iron
answered on 17 Feb 2012, 12:08 AM
Thanks Maya.
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).
0
Maya
Telerik team
answered on 17 Feb 2012, 08:08 AM
Hi Curt,

I am happy to see that the suggested approach fits into your requirements. Let me know in case you need further assistance. 

Kind regards,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Curt
Top achievements
Rank 1
Iron
Answers by
Maya
Telerik team
Curt
Top achievements
Rank 1
Iron
Share this question
or