Implementing INotifyCollectionChanged in CodeBehind

5 posts, 0 answers
  1. Clinton
    Clinton avatar
    22 posts
    Member since:
    May 2011

    Posted 24 Jan 2012 Link to this post

    Hi Team,

    I have a DataTable, which implements the INotifyCollectionChanged...

    public event NotifyCollectionChangedEventHandler CollectionChanged;
     
    protected virtual void OnCollectionChanged( )
    {
       if( CollectionChanged != null )
       {
          CollectionChanged( this, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Reset ) );
       }
    }

    However, the CollectionChanged is always equal to null......

    This is because the RadGridView doesn't know the CollectionChanged exists.

    We construct our RadGridView in codebehind and just need to wire it up to the CollectionChanged event.  How do I do this?

    Cheers

    Clinton
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 25 Jan 2012 Link to this post

    Hi,

     Can you clarify more about your DataTable. Is is this one?

    All the best,
    Vlad
    the Telerik team

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

  3. Clinton
    Clinton avatar
    22 posts
    Member since:
    May 2011

    Posted 29 Jan 2012 Link to this post

    Hi Vlad,

    We're using our own DataTable, I've downloaded a project with your one, which is very nice.  I hadn't seen the DynamicObject before.

    I think I have a better idea as to what's going on, I'll be looking at it today, if I have any further questions, I'll post them here.

    Cheers

    Clinton
  4. Clinton
    Clinton avatar
    22 posts
    Member since:
    May 2011

    Posted 29 Jan 2012 Link to this post

    Hi Vlad,

    I've made a few changes to the SilverlightDataTable project.  One of the things that was confusing me was the casting from the DataContext to a DataTable when adding a row.  This isn't needed to be done with the INotifyCollectionChanged as far as I can tell.  I thought this might help others also.

    Plus a catch for delete if no rows are selected:-

    using System;
    using System.Windows.Controls;
    using Telerik.Data;
    using System.Windows;
     
    namespace SilverlightDataTable
    {
       public partial class Page : UserControl
       {
          string[ ] names = new string[ ] { "Côte de Blaye", "Boston Crab Meat",
                "Singaporean Hokkien Fried Mee", "Gula Malacca", "Rogede sild",
                "Spegesild", "Zaanse koeken", "Chocolade", "Maxilaku", "Valkoinen suklaa" };
          decimal[ ] prizes = new decimal[ ] { 23.25M, 9.00M, 45.60M, 32.00M,
                14.00M, 19.00M, 263.50M, 18.40M, 3.00M, 14.00M };
          bool[ ] bools = new bool[ ] { true, false, true, false, true, false, true, false, true, false };
          DataTable table = null;
     
          private readonly Random rnd = new Random( );
     
          public Page( )
          {
             InitializeComponent( );
     
             table = new DataTable( );
             table.Columns.Add( new DataColumn( ) { ColumnName = "ID", DataType = typeof( int ) } );
             table.Columns.Add( new DataColumn( ) { ColumnName = "Name", DataType = typeof( string ) } );
             table.Columns.Add( new DataColumn( ) { ColumnName = "UnitPrice", DataType = typeof( decimal ) } );
             table.Columns.Add( new DataColumn( ) { ColumnName = "Date", DataType = typeof( DateTime ) } );
             table.Columns.Add( new DataColumn( ) { ColumnName = "Discontinued", DataType = typeof( bool ) } );
     
             for( int i = 0; i < 5; i++ )
             {
                var row = table.NewRow( );
                row[ "ID" ] = i;
                row[ "Name" ] = names[ rnd.Next( 9 ) ];
                row[ "UnitPrice" ] = prizes[ rnd.Next( 9 ) ];
                row[ "Date" ] = DateTime.Now.AddDays( i );
                row[ "Discontinued" ] = bools[ rnd.Next( 9 ) ];
                table.Rows.Add( row );
             }
     
             DataContext = table;
          }
     
          private void Button1_Click( object sender, System.Windows.RoutedEventArgs e )
          {
             var row = table.NewRow( );
             row[ "ID" ] = table.Rows.Count;
             row[ "Name" ] = names[ rnd.Next( 9 ) ];
             row[ "UnitPrice" ] = prizes[ rnd.Next( 9 ) ];
             row[ "Date" ] = DateTime.Now.AddDays( table.Rows.Count );
             row[ "Discontinued" ] = bools[ rnd.Next( 9 ) ];
             table.Rows.Add( row );
          }
     
          private void Button2_Click( object sender, System.Windows.RoutedEventArgs e )
          {
             if( RadGridView1.CurrentItem != null )
             {
                table.Rows.Remove( table.Rows[ RadGridView1.Items.IndexOf( RadGridView1.CurrentItem ) ] );
             }
             else
             {
                MessageBox.Show( "Please select a row to be deleted before pressing delete" );
             }
          }
       }
    }

    I'm looking at the differences in your DataTable and what the differences are in ours.  I think ours has a different structure which is why it doesn't work as well.

    Clinton
  5. Clinton
    Clinton avatar
    22 posts
    Member since:
    May 2011

    Posted 31 Jan 2012 Link to this post

    Hi Vlad,

    I have the implementation working correctly, thanks for your project it helped a lot.

    Clinton
Back to Top