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

Implementing INotifyCollectionChanged in CodeBehind

4 Answers 254 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Clinton
Top achievements
Rank 1
Clinton asked on 24 Jan 2012, 10:49 PM
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

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 25 Jan 2012, 08:32 AM
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 >>

0
Clinton
Top achievements
Rank 1
answered on 30 Jan 2012, 12:07 AM
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
0
Clinton
Top achievements
Rank 1
answered on 30 Jan 2012, 01:53 AM
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
0
Clinton
Top achievements
Rank 1
answered on 01 Feb 2012, 03:25 AM
Hi Vlad,

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

Clinton
Tags
GridView
Asked by
Clinton
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Clinton
Top achievements
Rank 1
Share this question
or