CellValidation get entity from GridViewComboBoxColumn

6 posts, 1 answers
  1. Curt
    Curt avatar
    15 posts
    Member since:
    Jan 2012

    Posted 14 Feb 2012 Link to this post

    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!
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 15 Feb 2012 Link to this post

    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 >>
  3. Curt
    Curt avatar
    15 posts
    Member since:
    Jan 2012

    Posted 15 Feb 2012 Link to this post

    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


  4. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 16 Feb 2012 Link to this post

    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 >>
  5. Curt
    Curt avatar
    15 posts
    Member since:
    Jan 2012

    Posted 16 Feb 2012 Link to this post

    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).
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 17 Feb 2012 Link to this post

    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 >>
Back to Top