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

Binding to a DataView

5 Answers 172 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 21 Oct 2010, 01:42 PM

Hi,

I am try to bind a RadGridView to DataView because of the Notify events that are already available. I am using the WPF MVVM architecture therefore the Grid is defined in the XAML and the DataView in my presentation Layer. I have a background worker that will populate the table in the DataView and I was hoping the RadGridView would then update accordingly. I am not too sure why it is not working or whether it can work in this context. Please see the code snippet below.

 


View:

<telerikGrid:RadGridView x:Name="activeSessionsGrid" AutoGenerateColumns="True" ItemsSource="{Binding Path=ActiveSessions}"

</telerikGrid:RadGridView> 


Presentation Model:

private DataView _activeSessions;

 

public DataView ActiveSessions

{

     get

     {

        return this._activeSessions;

     }           

}

private void GenereateTableStructure ( )

{

this._activeSessions = new DataView ( ); 

this._activeSessions.Table = new DataTable ( "Sessions" );

this._activeSessions.Table.Columns.Add ( "ID" );

this._activeSessions.Table.Columns.Add ( "Name" );

this._activeSessions.Table.Columns.Add ( "Surname" );

this._activeSessions.Table.Columns.Add ( "Designation" );

this._activeSessions.Table.Columns.Add ( "User Name" );

this._activeSessions.Table.Columns.Add ( "Email Address" );

this._activeSessions.Table.Columns.Add ( "Last Access Time" );
}

//BackgroundWorker that populates the _activeSessions table

 


Thanks in advance for your assistance.

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Oct 2010, 02:18 PM
Hi,

 You will need to raise manually PropertyChanged for this property (ActiveSessions).

Greetings,
Vlad
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
Chris
Top achievements
Rank 1
answered on 21 Oct 2010, 03:06 PM

Hi Vlad,

Please excuse my ignorant but I have made the following changes but to no avail.

 

public DataView ActiveSessions

{

   get

   {

     return this._activeSessions;

   }

   set

   {               

     this._activeSessions = value;

     PropertyChanged = new PropertyChangedEventHandler ( this.Notifychanges );

   }

}

 

 

private void Notifychanges ( object sender, PropertyChangedEventArgs e )

{

   if ( e.PropertyName != null )

       PropertyChanged ( this, new PropertyChangedEventArgs ( e.PropertyName ) );

}

 

public event PropertyChangedEventHandler PropertyChanged;

0
Vlad
Telerik team
answered on 21 Oct 2010, 03:10 PM
Hi,

 Are you still using the field directly? Here is the code from your previous post:

private void GenereateTableStructure ( )

{

this._activeSessions = new DataView ( ); 

...

You will need also just to raise the event not to create new event handler in the property setter. 

Greetings,
Vlad
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
Chris
Top achievements
Rank 1
answered on 21 Oct 2010, 03:25 PM
Hi,

Yes i am but the Property changed event is null as it has not been instantiated. I have included the entire class below.
public class ActiveSessionsPresentationModel:IActiveSessionsPresentationModel,INotifyPropertyChanged
    {
        private IShellController controller;
        private IActiveSessionsView view;
        private IDataBrokerService dbProxy;
        private BackgroundWorker worker;
        private DataView _activeSessions;
  
        public ActiveSessionsPresentationModel ( IShellController controller, IActiveSessionsView view, IDataBrokerService dbProxy )
        {
            this.controller = controller;
            this.view = view;
            this.dbProxy = dbProxy;
        }
  
        public IActiveSessionsView View
        {
            get
            {
                return this.view;
            }
        }
  
        public DataView ActiveSessions
        {
            get
            {
                return this._activeSessions;
            }
            set
            {                
                this._activeSessions = value;
                this.Notifychanges ( this, new PropertyChangedEventArgs ( "ActiveSessions" ) );
            }
        }
  
        public void DisplayActiveSessions ( )
        {
            ActiveSessionsResponse response = new ActiveSessionsResponse ( );
            response = this.dbProxy.ListActiveSessions ( );
  
            GenereateTableStructure ( );
  
            object [ ] obj = new object [ 1 ];
            obj [ 0 ] = response.ActiveSessions;
  
            this.worker = new BackgroundWorker ( );
            this.worker.DoWork += new DoWorkEventHandler ( worker_DoWork );
            this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler ( worker_RunWorkerCompleted );
            this.worker.RunWorkerAsync ( obj );
        }
  
        private void GenereateTableStructure ( )
        {
            this._activeSessions = new DataView ( );
            this._activeSessions.Table = new DataTable ( "Sessions" );
            this._activeSessions.Table.Columns.Add ( "ID" );
            this._activeSessions.Table.Columns.Add ( "Name" );
            this._activeSessions.Table.Columns.Add ( "Surname" );
            this._activeSessions.Table.Columns.Add ( "Designation" );
            this._activeSessions.Table.Columns.Add ( "User Name" );
            this._activeSessions.Table.Columns.Add ( "Email Address" );
            this._activeSessions.Table.Columns.Add ( "Last Access Time" );
        }
  
        private void worker_RunWorkerCompleted ( object sender, RunWorkerCompletedEventArgs e )
        {
            this.worker.Dispose ( );
        }
  
        private void worker_DoWork ( object sender, DoWorkEventArgs e )
        {
            object [ ] obj = (object []) e.Argument;
            Dictionary<Guid, DateTime> sessions = ( Dictionary<Guid, DateTime> ) obj [ 0 ];
  
            Parallel.ForEach ( sessions, pair =>
            {
                PersonResponse response = this.dbProxy.SelectPerson ( new PersonIDRequest ( )
                {
                    ID = pair.Key
                } );
  
                if ( response != null )
                {
                    _activeSessions.Table.NewRow ( );
                    DataRow row = _activeSessions.Table.NewRow ( );
                    row [ "ID" ] = pair.Key;
                    row [ "Name" ] = AccessControl.GetADUserFirstName ( response.Person.UserName );
                    row [ "Surname" ] = AccessControl.GetADUserSurname ( response.Person.UserName );
                    row [ "Designation" ] = AccessControl.GetADUserDesignation ( response.Person.UserName );
                    row [ "User Name" ] = response.Person.UserName;
                    row [ "Email Address" ] = AccessControl.GetADUserEmailAddress ( response.Person.UserName );
                    row [ "Last Access Time" ] = pair.Value;                    
                    _activeSessions.Table.Rows.Add ( row );
                    ActiveSessions = _activeSessions;
                      
                }
                else
                {
                    _activeSessions.Table.NewRow ( );
                    DataRow row = _activeSessions.Table.NewRow ( );
                    row [ "ID" ] = pair.Key;
                    row [ "Name" ] = string.Empty;
                    row [ "Surname" ] = string.Empty;
                    row [ "Designation" ] = string.Empty;
                    row [ "User Name" ] = string.Empty;
                    row [ "Email Address" ] = string.Empty;
                    row [ "Last Access Time" ] = pair.Value;
                    _activeSessions.Table.Rows.Add ( row );
                }
            } );            
        }
  
        #region INotifyPropertyChanged Members        
  
        private void Notifychanges ( object sender, PropertyChangedEventArgs e )
        {
            if ( e.PropertyName != null )
  
                PropertyChanged ( this, new PropertyChangedEventArgs ( e.PropertyName ) );
        }
  
        public event PropertyChangedEventHandler PropertyChanged;
  
        #endregion        
    }
  
    public interface IActiveSessionsPresentationModel
    {
        IActiveSessionsView View
        {
            get;
        }
        void DisplayActiveSessions ( );
        DataView ActiveSessions
        {
            get;
        }
    }
0
Chris
Top achievements
Rank 1
answered on 22 Oct 2010, 07:46 AM
Hi,

Would i not also need to change my binding to
ItemsSource="{Binding Path=ActiveSessions, Mode=TwoWay}"
Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Chris
Top achievements
Rank 1
Share this question
or