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

[Solved] GridView Validation problem when extending GridViewDataColumn Class

1 Answer 217 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Oscar De Ita
Top achievements
Rank 1
Oscar De Ita asked on 24 Jul 2009, 12:01 AM
Hi,

We are trying to find the best way to be able to maintain the grid view validation and its event CellValidating when extending the GridViewDataColum. Currently we have a grid that has 2 columns, one is a regular GridViewDataColumn and the other one is an extended class column we created that extended the GridViewDataColumn. On that custom class we overrided the CreateCellElement method. Due to that, we have lost the validation and we are trying to figure out how to recover it so that the grid still performs its validation.

Thanks
-Oscar

PS: Below is the code of our custom class

public class PropertyGridColumn : GridViewDataColumn 
    { 
        #region Declarations 
        private enum ElementToCreate { CheckBox, ComboBox, DatePicker, ListBox, TextBox } 
        #endregion 
        #region Methods : Public 
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) 
        { 
            FrameworkElement el = base.CreateCellEditElement(cell, dataItem); 
            PropertyGridItem item = (PropertyGridItem) dataItem; 
            Binding binding = new Binding() 
                                  { 
                                      Mode = BindingMode.TwoWay, 
                                      Source = item, 
                                      Path = new PropertyPath("Value"new object[] {}) 
                                  }; 
             
            if(item.ReadOnly) 
            { 
                return el; 
            } 
             
            switch (item.EditorType) 
            { 
                case EditorOption.Inherit: 
                    if (item.ValidValues.ItemsSource == null
                        if (item.ValueType == typeof(bool)) 
                            el = CreateFrameworkElementWithBinding(ElementToCreate.CheckBox, binding); 
                        else if (item.ValueType == typeof(DateTime)) 
                            el = CreateFrameworkElementWithBinding(ElementToCreate.DatePicker, binding); 
                        else if (item.ValueType == typeof(string
                                || item.ValueType.IsPrimitive) 
                            el = CreateFrameworkElementWithBinding(ElementToCreate.TextBox, binding); 
                        else if (item.ValueType.IsEnum) 
                        { 
                            el = CreateFrameworkElementWithBinding(ElementToCreate.ComboBox, binding,  
                                new PropertyGridControlValidValuesAttribute(){ItemsSource = EnumHelper.GetValues(item.ValueType)}); 
                        } 
                        else ; 
                    else 
                    { 
                        el = CreateFrameworkElementWithBinding(ElementToCreate.ComboBox, binding, item.ValidValues); 
                    } 
                    break
                case EditorOption.CheckBox: 
                    el = CreateFrameworkElementWithBinding(ElementToCreate.CheckBox, binding); 
                    break
                case EditorOption.ComboBox: 
                    el = CreateFrameworkElementWithBinding(ElementToCreate.ComboBox, binding, item.ValidValues); 
                    break
                case EditorOption.DatePicker: 
                    el = CreateFrameworkElementWithBinding(ElementToCreate.DatePicker, binding); 
                    break
                case EditorOption.ListBox: 
                    el = CreateFrameworkElementWithBinding(ElementToCreate.ListBox, binding, item.ValidValues); 
                    break
                case EditorOption.TextBox: 
                    el = CreateFrameworkElementWithBinding(ElementToCreate.TextBox, binding); 
                    break
            } 
            return el; 
        } 
        #endregion 
        #region Methods : Private 
        private static FrameworkElement CreateFrameworkElementWithBinding(ElementToCreate elementToCreate, Binding binding) 
        { 
            var dummyValue = new PropertyGridControlValidValuesAttribute(); 
            return CreateFrameworkElementWithBinding(elementToCreate, binding, dummyValue); 
        } 
 
        private static FrameworkElement CreateFrameworkElementWithBinding(ElementToCreate elementToCreate, Binding binding, PropertyGridControlValidValuesAttribute validValues) 
        { 
            switch (elementToCreate) 
            { 
                case ElementToCreate.CheckBox: 
                    { 
                        CheckBox cb = new CheckBox(); 
                        //cb.IsThreeState = false; 
                        cb.SetBinding(CheckBox.IsCheckedProperty, binding); 
                        return cb; 
                    } 
                case ElementToCreate.ComboBox: 
                    { 
                        //RadComboBox 
                        ComboBox cb = new ComboBox(); 
                        AddValidValuesBindingToFrameworkElement(cb, validValues); 
                        cb.SetBinding(ComboBox.SelectedItemProperty, binding); 
                        return cb; 
                    } 
                case ElementToCreate.DatePicker: 
                    { 
                        //RadDatePicker 
                        DatePicker dp = new DatePicker(); 
                        dp.SetBinding(DatePicker.SelectedDateProperty, binding); 
                        return dp; 
                    } 
                case ElementToCreate.ListBox: 
                    { 
                        //using statements 
                        ListBox lb = new ListBox(); 
                        AddValidValuesBindingToFrameworkElement(lb, validValues); 
                        lb.SetBinding(ListBox.SelectedItemProperty, binding); 
                        return lb; 
                    } 
                case ElementToCreate.TextBox: 
                    { 
                        TextBox tb = new TextBox(); 
                        tb.SetBinding(TextBox.TextProperty, binding); 
                        return tb; 
                    } 
                default
                    throw new ArgumentException("Invalid Element to Create""elementToCreate"); 
            } 
        } 
 
        private static void AddValidValuesBindingToFrameworkElement(Selector se, PropertyGridControlValidValuesAttribute validValues) 
        { 
            Binding binding = new Binding() 
                                  { 
                                      Mode = BindingMode.TwoWay, 
                                      Source = validValues, 
                                      Path = new PropertyPath("ItemsSource"new object[] {}) 
                                  }; 
 
            se.SetBinding(Selector.ItemsSourceProperty, binding); 
            if (!string.IsNullOrEmpty(validValues.DisplayMemberPath)) 
                se.DisplayMemberPath = validValues.DisplayMemberPath; 
            //if (!string.IsNullOrEmpty(validValues.SelectedValuePath)) 
                //se.SelectedValuePath = validValues.SelectedValuePath; 
        } 
        #endregion 
    } 

1 Answer, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 29 Jul 2009, 08:41 AM
Hello Oscar De Ita,

Indeed when you inherit from GridViewDataColumn you will lose built-in validation at the UI (RadGridView) level, so you need a data layer level validation. We are working on this feature unfortunately it will be available with the next official release (2009.Q3).

As a workaround you can try to include validation logic to the editing elements (TextBox, ComboBox and etc.) of the custom data column.

Best wishes,
Nedyalko Nikolov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Oscar De Ita
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Share this question
or