Sorting on column with contains a changed cell

6 posts, 0 answers
  1. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 28 Dec 2011 Link to this post

    Hi

    I am trying to sort a Int32 column and it fails if any cell in the column is changed (Same for Group Panel).  If I sort the column without cell changes no problem.  I tried both creating the columns in xaml and in code with not difference.  Also i am binding to an array of object.  I also tried both Array and List.  Also, the columns that are String sort properly when a cell is changed in the column.  Am i missing something?

    The code is as follows.

    1. Create data class
    2. View Model class
    3. Main Page code behind
    4. Xaml

    Thanks
    Rich
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    using System;
    namespace RadGridView_SL4_AR_25
    {
        public class CRow
        {
     
            private List<object> _Columns = new List<object>();
     
            public List<object> Columns
            {
                get { return _Columns; }
                set { _Columns = value;}
            }
     
            public static ObservableCollection<CRow> GetRows()
            {
                ObservableCollection<CRow> rows = new ObservableCollection<CRow>();
                CRow row;
                List<object> columns = new List<object>();
                columns.Add("Lawrence Taylor");
                columns.Add("7230 Surfbird Lane");
                columns.Add("San Diego");
                columns.Add("CA");
                columns.Add(92011);
     
                row = new CRow() { Columns = columns };
                rows.Add(row);
     
                columns = new List<object>();
                columns.Add("Phil Simms");
                columns.Add("1800 Quarteback Way");
                columns.Add("New York");
                columns.Add("NY");
                columns.Add(73418);
     
                row = new CRow() { Columns = columns };
                rows.Add(row);
     
                columns = new List<object>();
                columns.Add("Mark Bavaro");
                columns.Add("412 End Lane");
                columns.Add("Philadelphia");
                columns.Add("PA");
                columns.Add(45722);
     
                row = new CRow() { Columns = columns };
                rows.Add(row);
      
                return rows;
            }
        }
     
        public class ColumnMetadata
        {
            string _UniqueName;
            public string UniqueName
            {
                get { return _UniqueName; }
                set { _UniqueName = value; }
            }
     
            string _Header;
            public string Header
            {
                get { return _Header; }
                set { _Header = value; }
            }
     
            string _DataType;
            public string DataType
            {
                get { return _DataType; }
                set { _DataType = value; }
            }
     
            public static ObservableCollection<ColumnMetadata> GetColumnMetadata()
            {
                ObservableCollection<ColumnMetadata> cols = new ObservableCollection<ColumnMetadata>();
     
                ColumnMetadata columnMetadata = new ColumnMetadata();
                columnMetadata.UniqueName = "Customer_Name";
                columnMetadata.Header = "Name";
                columnMetadata.DataType = "System.String";
                cols.Add(columnMetadata);
     
                columnMetadata = new ColumnMetadata();
                columnMetadata.UniqueName = "Customer_Street";
                columnMetadata.Header = "Street";
                columnMetadata.DataType = "System.String";
                cols.Add(columnMetadata);
     
                columnMetadata = new ColumnMetadata();
                columnMetadata.UniqueName = "Customer_City";
                columnMetadata.Header = "City";
                columnMetadata.DataType = "System.String";
                cols.Add(columnMetadata);
     
                columnMetadata = new ColumnMetadata();
                columnMetadata.UniqueName = "Customer_State";
                columnMetadata.Header = "State";
                columnMetadata.DataType = "System.String";
                cols.Add(columnMetadata);
     
                columnMetadata = new ColumnMetadata();
                columnMetadata.UniqueName = "Customer_ZipCode";
                columnMetadata.Header = "Zip Code";
                columnMetadata.DataType = "System.Int32";
                cols.Add(columnMetadata);
     
                return cols;
            }
        }
    }

    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace RadGridView_SL4_AR_25
    {
        public class MyViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
             
            private ObservableCollection<CRow> _GridItems;
     
            public ObservableCollection<CRow> GridItems
            {
                get
                {
                    return CRow.GetRows();
                }
     
                set
                {
                    _GridItems = value;
                    OnPropertyChanged("GridItems");
     
                }
     
            }
             
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, args);
                }
            }
     
            private void OnPropertyChanged(string propertyName)
            {
                this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Data;
    using System.Collections.ObjectModel;
    using System.Windows.Data;
     
    namespace RadGridView_SL4_AR_25
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();  
            }
     
            private void myGrid_Loaded(object sender, RoutedEventArgs e)
            {
                ObservableCollection<ColumnMetadata> columns = ColumnMetadata.GetColumnMetadata();
     
                for (int i = 0; i < columns.Count; i++)
                {
                    myGrid.Columns.Add(new GridViewDataColumn()
                    {
                        Header = columns[i].Header,
                        UniqueName = columns[i].UniqueName,
                        DataMemberBinding = new Binding("Columns[" + i + "]") { Mode = BindingMode.TwoWay },
                        DataType = Type.GetType(columns[i].DataType)
                    });
                }
            }
        }
    }

    <UserControl x:Class="RadGridView_SL4_AR_25.MainPage"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:my="clr-namespace:RadGridView_SL4_AR_25"
                 xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
                 mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700">
        <UserControl.Resources>
            <my:MyViewModel x:Key="MyViewModel"/>      
        </UserControl.Resources>
         
         
        <Grid x:Name="LayoutRoot"
              Background="White"
              DataContext="{StaticResource MyViewModel}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
                     
     
            <telerik:RadGridView
                Name="myGrid" Grid.Row="0"
                ItemsSource="{Binding GridItems, Mode=TwoWay}"
                CanUserSortColumns="True"
                ShowGroupPanel="True"
                Loaded="myGrid_Loaded" 
                AutoGenerateColumns="False">
            </telerik:RadGridView>
                 
        </Grid>
    </UserControl>

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 30 Dec 2011 Link to this post

    Hello Richard,

    Thank you for the provided code snippets.

     It seems that the problem originates in your implementation of the data (and so the GridViewDataColumn cannot resolve the right type for the bound object). Please check this blog post on what is the recommended approach on this matter.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 30 Dec 2011 Link to this post

    Hi,

    I referenced the following:

    http://www.telerik.com/community/forums/silverlight/gridview/cannot-group-or-sort.aspx

    I wrote my code pretty much the way it is  specified in the above reference and my code also allows the columns to sort and group properly.  Also I can filter on the Int32 column and based on the compare choices I got from the filter popup the filter knows the field is numeric.  The columns that are String show "contains" etc and the Int32 column does not.  So everything works fine until I change the value of a cell in one of Int32 columns and then try and sort or group that column.  It crashes at this point.

    All the code is in my previous post if could try it out.  I  wanted to send the whole app in a zip file but that format was not accepted.

    Thanks
    Rich

  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 03 Jan 2012 Link to this post

    Hello Rich,

    Thank you for providing this additional information. Still I cannot figure out how this would be resolved.

     In my previous post I have missed to link to the pointed blog. Would you try to model your data as explained in the blog? Hopefully this will solve the problem.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 03 Jan 2012 Link to this post

    Hi Didie,

    Was your blog reference supposed to be the lightweight data table?  I can see how the light-weight data table may work. However the problem I have with the datatable is that it does not work for WP7 and I am trying to be compatible with WP7 as much as possible. 

    Are we saying that a gridview item that is bound to an array of objects will display properly and also sort and group properly,  but then when a int object is changed that it will not sort or group (in my case crashes).    Rather then trying to use my code would it be possible for you to create a simple demo that demonstrates weather changing a cell in this scenario works or not. If you can not get this to work maybe this should be then be considered  a bug.

    Thanks
    Rich
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Jan 2012 Link to this post

    Hello,

     Can you verify if your scenario will work normally with standard Silverlight DataGrid?

    Kind regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top