none
How to use a single datatabe in multiple places? RRS feed

  • Question

  • I have an app with several tabs that presents data from a database and allows users to edit, add and delete records from the database.

    I need to use some dialogs for users to select from and edit some tables. My problem is that I don't see how to get from the dialog to  the copy of the data already in the datatable. When I am in the dialog how do I populate a listbox and access a datarow from an existing datatable that already is loaded via a fill() statement when the application starts.

    In the MainWindow class I have:

    public IntlDataSetTableAdapters.ProviderCompaniesTableAdapter intlDataSetProviderCompaniesTableAdapter { get; set; }
    
    

    In Window_Loaded of the MainWindow i have:

    EditInternational.IntlDataSet intlDataSet = ((EditInternational.IntlDataSet)(this.FindResource("intlDataSet")));
    
    
    

    Then I have:

    intlDataSetProviderCompaniesTableAdapter = new EditInternational.IntlDataSetTableAdapters.ProviderCompaniesTableAdapter();
    intlDataSetProviderCompaniesTableAdapter.Fill(intlDataSet.ProviderCompanies);
    System.Windows.Data.CollectionViewSource providerCompaniesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("providerCompaniesViewSource")));
    providerCompaniesViewSource.View.MoveCurrentToFirst();
    

    This sets up the tableadapter so I can access it from one of the tabs, which works fine. The problem is that when I create a dialog in response to a button click eg.

     dlgSelectProvider dlg = new dlgSelectProvider(0);
    
    

    I need to recreate a tableadapter and run fill to get another copy of the data. Obvious a performance issue.

    I need to know how to access the existing tableadapter from my dialog box and can't figure out how.

    What am I missing here?

    Thanks,

    Glen

     

     


    • Moved by Figo Fei Monday, November 15, 2010 7:40 AM (From:Visual C# General)
    Friday, November 12, 2010 3:58 PM

All replies

  • Further:

    In the dialog I have:

     

    EditInternational.IntlDataSet intlDataSet = ((EditInternational.IntlDataSet)(this
    
    .FindResource("intlDataSet"
    
    )));
    EditInternational.IntlDataSetTableAdapters.ProviderCompaniesTableAdapter intlDataSetProviderCompaniesTableAdapter = 
    new
    EditInternational.IntlDataSetTableAdapters.ProviderCompaniesTableAdapter(); intlDataSetProviderCompaniesTableAdapter.Fill(intlDataSet.ProviderCompanies); providerCompaniesListView.IsSynchronizedWithCurrentItem = true ; providerCompaniesViewSource.View.MoveCurrentToFirst();

     

    This works but requires another trip to the database to fill the tableadapter again. I want to use a global copy instead of continually creating local copies. I probably need to move the definition of something but I am not that familiar with c# to know what.

    how do I accomplish this?

     

     

     

    Friday, November 12, 2010 6:34 PM
  • Hello,

    Please try to make another class in your project to share those DataTables  between different forms/tabs. Please take a look at the following project for an example:
    http://www.codeproject.com/KB/cs/WindowsformSharingData.aspx

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Tuesday, November 16, 2010 10:44 AM
  • Thanks Roahn, but that does not  quite solve my issue.

    I have a listview - providerCompaniesListView when I try to get a datarow from the selection with:

    EditInternational.IntlDataSet
    .ProviderCompaniesRow
     row = 
    (EditInternational.IntlDataSet .ProviderCompaniesRow )providerCompaniesListView.Items.CurrentItem;

    I get the error "Unable to cast object of type 'System.Data.DataRowView' to type 'ProviderCompaniesRow'."

    How do I get the row from the underlying data table corresponding to the selection in the listview?

    Thanks, Glen

    Tuesday, November 16, 2010 2:57 PM
  • Hi Glen,

    The error occurs because the providerCompaniesListView.Items.CurrentItem  is of type System.Data.DataRowView, however, you attempted to convert it to a Row class

    Please try to use the Row property of the DataRowView class to to the convert:
    http://msdn.microsoft.com/en-us/library/system.data.datarowview.row.aspx

    Best regards,


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Wednesday, November 17, 2010 3:33 AM
  • that's what I can't do

    From Intellisense:
    providerCompaniesListView is a ListView
    providerCompaniesListView.Items is ItemCollection Itemscontrol.Items
    providerCompaniesListView.Items.CurrentItem is current item in the view

    providerCompaniesListView.Items.CurrentItem.Row Says object Does not contain a definition for Row

    I get the same from (DataRowView)(providerCompaniesListView.Items.CurrentItem).Row

    Same from providerCompaniesListView.View.Row

    So what construct do I need to use to get to the row?
    The info shows up in the providerCompaniesListView, I just can't get to it in code
    Thursday, November 18, 2010 9:04 PM