views:

756

answers:

1

I have an ASPxGridView from DevExpress fed with data from ObjectDataSource. My data row objects expose properties such ParameterName, ParameterType and ParameterValue.

//Properties, constructor and private fields code omitted for clarity
public class InputParameterDescription
{
   public string ParameterName;

   public Type ParameterType;

   public int ParameterPrecision;

   public string ParameterDescription;
}

ParameterValue is always an object of type indicated by ParameterType property. In fact, I use few types – Int32, Double, String or Boolean. When I display values in a grid and user clicks “Edit” a ParameterValue is always edited with TextBox. Is it possible to change editor for this column according to ParameterType? I want my users to use SpinEdit for integers, checkbox for Boolean, etc.

In fact, this is the way people have been working with DevExpress Delphi grids - TdxGrid and TcxGrid (OnGetProperties event). I have asked this question in DevExpress forum, but haven’t got any answer :(

A: 

You could create a template on that column that would do the switch for you. Something like:

public class SwitchTemplate : ITemplate
{
   public void Instantiate(Control container)
   {
      GridViewDataItemTemplateContainer cnt = (GridViewDataItemTemplateContainer) container;
      switch( GetStringParameterTypeFromDataItem(cnt.DataItem) )
      {
         case "Int32":
            container.Controls.Add( new ASPxSpinEdit() { ... } );
            break;

         case "DateTime":
            container.Controls.Add( new ASPxDateEdit() { ... } );
            break;

         case "String":
            container.Controls.Add( new ASPxTextBox() { ... } );
            break;

         ...
      }  
   }
}

Then you just need to specify this template as the EditItemTemplate of the column:

myGrid.Columns["MyColumnName"].EditItemTemplate = new SwitchTemplate()
Locksfree