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

[Solved] edit value of cell of type object, will result a value of type string

2 Answers 172 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.
Alexander
Top achievements
Rank 1
Alexander asked on 20 Oct 2010, 02:53 PM
hi,

i have a problem with the editor functionallity of the GridView.
when i create a GridView and put cells in of value type of Object by TwoWay-binding and when the objects contains non-string values, after editing the cells inside the gridView the object type of the value is from type String.

here is my GridView definition...
<telerik:RadGridView
    x:Name="gridView"
    CanUserFreezeColumns="False"
    AutoGenerateColumns="False"
    ShowGroupPanel="False"
    RowIndicatorVisibility="Collapsed"
    >
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Param" DataMemberBinding="{Binding Param}" IsFilterable="False" IsReadOnly="True"/>
        <telerik:GridViewDataColumn Header="Value" IsFilterable="False" IsReadOnly="False" EditTriggers="CurrentCellClick">
            <telerik:GridViewDataColumn.DataMemberBinding>
                <Binding
                    Path="Value"
                    Mode="TwoWay"
                    />
            </telerik:GridViewDataColumn.DataMemberBinding>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

... my item element definition
public class ListElement
    : INotifyPropertyChanged
{
    #region Param Property
    public string Param { get; set; }
    #endregion
  
    #region Value Property
    public const string ValuePropertyName = "Value";
    public object ValueField;
    public object Value
    {
        get { return ValueField; }
        set
        {
            object oldValue = ValueField;
            object newValue = value;
  
            if (!object.ReferenceEquals(oldValue, newValue))
            {
                ValueField = newValue;
                RaisePropertyChanged(ValuePropertyName);
            }
        }
    }
    #endregion
  
    #region ValueType Property
    public Type ValueType { get; set; }
    #endregion
  
    public ListElement(string param, object value)
    {
        this.Param = param;
        this.Value = value;
  
        if (value != null)
            this.ValueType = value.GetType();
    }
  
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
  
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
            handler(this, args);
        }
    }
    #endregion
}

... and here are some test items
{
    ObservableCollection<ListElement> data = new ObservableCollection<ListElement>();
  
    data.Add(new ListElement("my Int32", (Int32)123));
    data.Add(new ListElement("my Int16", (Int16)12));
    data.Add(new ListElement("my Decimal", (Decimal)123.45M));
    data.Add(new ListElement("my Double", (Double)123.456));
    data.Add(new ListElement("my String", (String)"ABCDEF"));
  
    this.gridView.ItemsSource = data;
}

e.g.: if i try to edit the Value-cell of the "my Decimal" item, after editing the value is from type String.
but i want to have a value of the same type.

i tried to buld an Converter that will convert the string in its wanted value types, but than i have the problem that the TargetType is always from type Object - so i don't know inside the converter to what type i really want to convert the value.

i tried to bind the ConverterParameter to "ValueType" of the item, to see the wanted type inside the Converter, but the Binding throws an InvalidOperationException.

i tried to handle the gridView.CellValidating event, but i am not able to change the value of NewValue (is readonly), and IsValid & IsHandled will only affecting the commitment of the value that comes from the EditorPresenter(?).

i tried to handle the gridView.PreparingCellForEdit event, to apply an other EditorPresenter for the current cell, but there are no information what for this event is - i don't found any information about this event. on the way, i don't think that these is the correct place to adjust the editor of the cell...

i also tried to make an ListElement<T> (ListElement<Int32>, ListElement<Int16>, ListElement<Decimal>, ... with public <T> Value{} ), but than i cannot build an ObservableCollection<ListElement<T>> data = new ObservableCollection<ListElement<T>>();


does anybody has an idea, how i can specify the kind of cell editing if the targetType of the cell is from type Object?

2 Answers, 1 is accepted

Sort by
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 21 Oct 2010, 11:27 AM
Hi Alexander,

You could use GridViewDataColumn.CellEditTemplateSelector property. Take a look at this online example for more information.

Sincerely yours,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alexander
Top achievements
Rank 1
answered on 21 Oct 2010, 11:57 AM
Hi Nedyalko Nikolov,

thanks, but i don't think, that this will help me. or i have to adapt the CellEditSelector in that way, that it will switch its behavour depending of the object type of the cell.

my grid has a column of cells with different object value types - one column / different rows - not one row with different columns.

i found a mybe very dirty solution, that works for me good enough.
now, i am using the
gridView.BeginningEdit
event to look what type the real type of my cell value is, an change it from typeof(object) to the wanted type e.g. typeof(Decimal)
void gridView_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
{
    ListElement element = e.Row.Item as ListElement;
    if (element != null)
        e.Cell.DataColumn.DataType = element.ValueType;
}

and with the event
gridView.CellValidating
i make some tests to check the types of old and new value, and theire string representations from the converted value and the TextBoxElement.Text representations. if passed every thing is okay for me, and the resulting type stays with the type before. if not passed i set IsValid to false... and the changed value will not commited.
private void gridView_CellValidating(object sender, GridViewCellValidatingEventArgs e)
{
    // no, better i don't show it... too stupid
}

i know... not an elegant way, but for me it works.
Tags
GridView
Asked by
Alexander
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Alexander
Top achievements
Rank 1
Share this question
or