Home Forums WPF controls Xceed DataGrid for WPF Access the grid control from the App.cs

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • User (Old forums)
    Post count: 23064
    #22890 |

    I apologize for my English first and foremost.

    My application’s input is not through typing values into the grid, but rather through a different process, which inserts rows into the database.
    After adding a new row to the database the grid does not refresh itself even though it is bound to the DataTable which has just been updated.

    Therefor I wish to have a handle to the grid in the App.cs (where the input process runs) to enable me to use something like:

    My grid resides in MainWindow.xaml.
    The input process runs on App.cs.

    When it finishes inserting a row I try using (within App.cs):
    DataGridControl grid = ( DataGridControl )( this.MainWindow.FindName( “myGrid” ) );
    but I get InvalidOperationException (The calling thread cannot access this object because a different thread owns it).

    Plese help.

    Imported from legacy forums. Posted by Golan (had 2476 views)

    Xceed Support
    Post count: 5658

    Concerning the fact that the DataGridControl does not automatically show newly inserted items…

    I’d like to have a clarification about your implementation. Are you adding the items directly in the database or do you pass through the DataTable object.

    Normally, we receive a notification of addition when an item is added in the DataView (which the the “CollectionView” used by the DataGridControl to display a DataTable ( see DataTable.DefaultView). If this notification is effectivelly not send and you are updating the DataTable… Then I’d like to have more details.

    Then, concerning the InvalidOperationException… UI Elements can only be accessed from the UI thread… One easy way to ensure you are executing on the UI Thread is to use the dispatcher for the UI element in question:

    grid.Dispatcher.Invoke() and grid.Dispatcher.BeginInvoke() should do the trick!

    I’ll be waiting for your feedback concerning the addition of items in the DataTable.

    Note: Don’t worry, your English is very good!

    Imported from legacy forums. Posted by Marcus [Xceed] (had 263 views)

    User (Old forums)
    Post count: 23064

    Marcus, you are the best!
    Thank you!
    The Dispatcher.Invoke() was just what I needed.
    Here is the code that did the trick:

    using System;

    namespace WindowsApplication1
    public delegate void DelegateZeroParam( );

    public partial class App : System.Windows.Application

    void OnInputReady( DataObject newRecord)
    this.dataManager.AddRecord(newRecord); // add the new record to the database
    this.Dispatcher.Invoke( System.Windows.Threading.DispatcherPriority.Normal,
    ( DelegateZeroParam )delegate( )
    ((MainWindowClass)this.MainWindow).gridEntries.Items.Refresh( );
    } );

    MainWindowClass is the name of my main window class, where the DataGridControl (gridEntries) resides.
    After updating the database the Dispacher invokes a delegate function, which now has access to the grid. All that’s left to do is tell the grid to refresh it’s items.

    As for your question. Yes I use the DataTable to insert new DataRow into the database.

    App.cs has a dataManager object which holds inside a DataSet.
    App.cs exposes the DataTables inside the dataManager.dataSet property, as public DataTable properties.
    public DataTable Entries
    get{ return this.dataManager.dataSet.Tables[“Entries”]; }

    On MainWindow.xaml the binding is done like so:

    <xcdg:DataGridControl x:Name=”gridEntries” ItemsSource=”{Binding Source={x:Static Application.Current}, Path=Entries}”/>

    the updating of the database is actually done in a different Data Access Layer project within this solution. The product of this project is the dataManager object I use in App.cs.
    I use predefined stored procedures for handling all database functionalities.

    Again, thank you Marcus.
    I was not aware of the UI thread in WPF, nor to using the Dispatcher.
    I hop this post will help others having the same problem as I did.

    Imported from legacy forums. Posted by Golan (had 3621 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.