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

Custom combobox edit column template

2 Answers 58 Views
Grid
This is a migrated thread and some comments may be shown as answers.
solutionfactory
Top achievements
Rank 1
solutionfactory asked on 30 Oct 2008, 10:23 AM
Hi all,
i have a problem with my custom edit column template.

This is my column
        public class MyEnumColumnEditor : GridTextColumnEditor 
        { 
            private RadComboBox box; 
            private readonly Type enumType; 
            private readonly bool useDescription = false
            private readonly bool addTopEmptyItem = false
 
 
            public MyEnumColumnEditor(Type enumType, bool useDescription, bool addTopEmptyItem) 
            { 
                this.enumType = enumType; 
                this.useDescription = useDescription; 
                this.addTopEmptyItem = addTopEmptyItem; 
            } 
 
            protected override void AddControlsToContainer() 
            { 
                this.box = new RadComboBox(); 
 
                ArrayList list = new ArrayList(); 
                Array enumValues = Enum.GetValues(enumType); 
                Type baseType = Enum.GetUnderlyingType(enumType); 
 
                if (addTopEmptyItem) 
                    list.Add(new KeyValuePair<intstring>(-1, string.Empty)); 
 
                foreach (Enum value in enumValues) 
                { 
                    string desc = useDescription ? Data.GetEnumDescription(value) : value.ToString(); 
                    list.Add(new KeyValuePair<intstring>((int)Convert.ChangeType(value, baseType), desc)); 
                } 
 
                this.box.DataSource = list; 
                this.box.DataTextField = "Value"
                this.box.DataValueField = "Key"
                this.box.DataBind(); 
                this.box.SelectedIndex = 0; 
                 
                this.ContainerControl.Controls.Add(this.box); 
            } 
 
            protected override void LoadControlsFromContainer() 
            { 
                this.box = this.ContainerControl.Controls[0] as RadComboBox; 
            } 
 
            public override bool IsInitialized 
            { 
                get 
                { 
                    return this.box != null
                } 
            } 
 
            public override string Text 
            { 
                get 
                { 
                    if (this.box.SelectedItem == null
                        return string.Empty; 
 
                    return this.box.SelectedValue; 
                } 
                set  
                { 
                    this.box.SelectedIndex = string.IsNullOrEmpty(value) ? 0 :  
                                           this.box.FindItemIndexByValue(value); 
                } 
            } 
 
        } 
 

All works fine, but when i edit an existing item, the selected item displayed on combobox is not the current value.

The SelectedIndex is correctly but the displayed item is always the first.

Any suggestion?

Thanks in advance,
Flavio



2 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay Rusev
Telerik team
answered on 03 Nov 2008, 03:26 PM
Hello Flavio,

Please find the project attached to this forum post. It demonstrates how you must implement custom column editor. The links below will give you a detailed information about how grid editors works and how to extend them.

Custom editors extending auto-generated editors
Using grid server-side API for extraction

I hope that helps.

Best wishes,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
solutionfactory
Top achievements
Rank 1
answered on 03 Nov 2008, 05:06 PM
Hello Nikolay,
works as expected for me,
thank a lot.

Best regards,
Flavio
Tags
Grid
Asked by
solutionfactory
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
solutionfactory
Top achievements
Rank 1
Share this question
or