none
Refresh data in child datatable RRS feed

  • Question

  • Hi,

    I have two DataTables: an Order DataTable and an OrderItem DataTable. In the Order DataTable the amount of data is restricted by a parameter, so I will only get only the orders of one supplier.

    The OrderItems DataTable has a DataRelation with the Order DataTable.

    I have two Screens. In screen one the Order en OrderItems data is displayed, in Screen two the OrderItem data is modified.  Both screens do not  share the OrderItems DataTable, so they have different OrderItems DataTable instances.

    After I'v done some modifications to the OrderItem data in the "Modify" screen, I want to refresh the data in the "display" screen. I can do this by filling the OrderItems DataTable/DataTableAdapter.

    In this way however, I query all my OrderItems, not only the OrderItems belonging to my order, not only the OrderItems belonging to the Supplier, but all OrderItems in the Database.

    Isn't there another way for refreshing my OrderItems data, so that only the OrderItems belong to the current order are requeried/refreshed?

    Regards,

    Matthieu

     

    Thursday, May 13, 2010 10:24 AM

All replies

  • First, why can you not use the same OrderItems DataTable instance in both screens? Simply pass it as a parameter to the Modify screen. Then this whole problem will mostl likely disappear because whatever gets modified in the Modify screen is the same instance of the data that gets displayed on the first screen.

    In this way however, I query all my OrderItems, not only the OrderItems belonging to my order, not only the OrderItems belonging to the Supplier, but all OrderItems in the Database

    Why do you have to query all OrderItems instead of just the ones you are interested in? Are using TableAdapters or DataAdapters?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, May 15, 2010 11:01 PM
  • Hi Bonnie,

    "First, why can you not use the same OrderItems DataTable instance in both screens? "

    => The reason for not using the same datatable is that I want to reuse this screen in another place in my application. This won't be the case with the Order Screen but I could reuse a supplier screen. Probably a supplier will be displayed in multiple screens ( in the order screen, the bill screen etc.) I can make a button which allows the user to change the displayed supplier. In this scenario the supplier screen should have an independent datasource.

    "Why do you have to query all OrderItems instead of just the ones you are interested in?"

    => I don't want to query all the order items. My question is about queriing only the order items belonging to one order. How can this be achieved?

    "Are using TableAdapters or DataAdapters?"

    => I'm using TableAdapters/DataTables in a DataSet

    Thursday, May 20, 2010 11:00 AM
  • Hi Matthieu,

    "First, why can you not use the same OrderItems DataTable instance in both screens? "

    => The reason for not using the same datatable is that I want to reuse this screen in another place in my application. This won't be the case with the Order Screen but I could reuse a supplier screen. Probably a supplier will be displayed in multiple screens ( in the order screen, the bill screen etc.) I can make a button which allows the user to change the displayed supplier. In this scenario the supplier screen should have an independent datasource.

    I'm sorry, I'm still unclear on what you're trying to do. The scenario you described for the Supplier screen sounds different than what you initially described for the Order screen. In the case of what you're calling the Supplier screen, it sounds more like you want a UserControl containing Supplier information, rather than a full-blown Form (the UserControl would be dropped on a Form). Define a UserControl with all the TextBoxes you need for the Supplier and define a DataBind method using a SupplierDataTable as a parameter.

    public partial class SupplierUserControl : UserControl
    {
    	protected MyDataSet.SupplierDataTable dtSupplier = null;
    
    	public SupplierUserControl()
    	{
    		InitializeComponent();
    	}
    	public void DataBind(MyDataSet.SupplierDataTable dt)
    	{
    		this.dtSupplier = dt;
    		
    		this.txtSupplierName.DataBindings.Add("Text", dt, "SupplierName");
    		this.txtSupplierAddress.DataBindings.Add("Text", dt, "SupplierAddress");
    		// etc.etc.etc.
    	}
    }

    Then, you'd drop the SupplierUserControl onto your Order form and call its DataBind() method, passing it a SupplierDataTable. It could be the table from your current DataSet, or you could even have a separate DataSet/DataTable. But, my point is that you can still bind all the controls in your SupplierUserControl to the same DataTable *definition* ... you just might pass different instances of the SupplierDataTable into the DataBind() method.

    "Why do you have to query all OrderItems instead of just the ones you are interested in?"

    => I don't want to query all the order items. My question is about queriing only the order items belonging to one order. How can this be achieved?

    "Are using TableAdapters or DataAdapters?"

    => I'm using TableAdapters/DataTables in a DataSet

    Now, this is another reason I don't like (nor do I use) TableAdapters. They are not very flexible. If I was using a DataAdapter, I'd simply use a different SELECT statement to fill the DataSet/DataTable (actually, I always use Stored Procedures, but that's a different topic). You might want to take a look at my 3-part blog series on DataAccess (which also includes a link to my rant against TableAdapters):

    http://geek-goddess-bonnie.blogspot.com/2009/09/dataaccess-part-i.html
    http://geek-goddess-bonnie.blogspot.com/2009/10/dataaccess-part-ii.html
    http://geek-goddess-bonnie.blogspot.com/2009/10/dataaccess-part-iii.html

    I know this doesn't really answer your TableAdapter requery question, and perhaps someone else who is more familiar with TableAdapters might have a nifty way to do it, but I still think your best bet is to not have to do the re-query in the first place (by using the same DataTable object in both places).


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Friday, May 21, 2010 2:29 PM