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

[Solved] Generating runtime columns with custom editors

2 Answers 159 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.
Inquisitor Jax
Top achievements
Rank 1
Inquisitor Jax asked on 03 Sep 2009, 02:07 PM
I would like to know what is the best practice to do the following:

The columns are generated runtime. (easily done from previous examples I've seen - just set "AutoGenerateColumns" to false, and then add columns of type GridViewDataColumn to the columns collection.
But, I would also like to add columns at runtime that have custom CellEditTemplates - how would I go about this?

ex.: I want my grid to bind to a collection of StaffMember objects.
The columns need to be added at runtime to the grid, and, for the "Age" property, I would
like the CellEditTemplate to be a NumericUpDown Control.

Ideally, I would have liked to implement my own GridViewNumericColumn that inherits from GridViewDataColumn,
and override the CellEditTemplate to use the NumericUpDownControl.
I could then add this column to the grid.Columns collection at runtime.

2nd prize would just to create a new GridViewDataColumn, set the CellEditTemplate to the NumericUpDown control, and then add the column to the Columns collection.

2 Answers, 1 is accepted

Sort by
0
Inquisitor Jax
Top achievements
Rank 1
answered on 03 Sep 2009, 02:20 PM
okay - I seem to have figured out the basics.

Here's the code for those interested:
public class GridViewNumericColumn : GridViewDataColumn
    {
        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            RadNumericUpDown retTemplateControl = new RadNumericUpDown();
            //make sure to set the binding, else it won't work
            retTemplateControl.SetBinding(RadRangeBase.ValueProperty, DataMemberBinding);

            return retTemplateControl;
        }
    }


0
Inquisitor Jax
Top achievements
Rank 1
answered on 03 Sep 2009, 02:23 PM
PS: Here's some rudimentary generic code for building columns at runtime:

//Extension method
        public static void CreateColumnsForType<T>(this RadGridView grid, Dictionary<string, GridViewDataColumn> columnOverrides, List<string> propertiesToExclude)
        {
            grid.Columns.Clear();
            PropertyInfo[] propertyInfos = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                GridViewDataColumn column;

                if (propertiesToExclude != null && propertiesToExclude.Contains(propertyInfo.Name))
                {
                    continue;
                }

                if (columnOverrides !=null && columnOverrides.ContainsKey(propertyInfo.Name))
                {
                    column = columnOverrides[propertyInfo.Name];
                }
                else
                {
   //NOTE: Add more "if" statements for other data-type specific columns
                    if (propertyInfo.PropertyType == typeof(int?) || propertyInfo.PropertyType == typeof(int))
                    {
                        column = new GridViewNumericColumn();
                    }
                    else
                    {
                        column = new GridViewDataColumn();
                    }                    
                }


                column.UniqueName = propertyInfo.Name;
                column.Header = propertyInfo.Name;

                grid.Columns.Add(column);

            }
        }

Tags
GridView
Asked by
Inquisitor Jax
Top achievements
Rank 1
Answers by
Inquisitor Jax
Top achievements
Rank 1
Share this question
or