Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / September 2006

Tip: Looking for answers? Try searching our database.

Problem: Master / Detail + DataRelation + DataView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arif - 17 Sep 2006 12:38 GMT
Hi all,

Testing with
Database : Nortwind
Master Table : Customers
Detail Table : Orders

DataRelation : relCustomersOrders

On Form_Load, DataAdapter fills the dataset with above two table and creates
a DataRelation. Everything is OK. When I click on a row of
MasterGrid(Customers) then DetailGrid(Orders) automatically shows the related
records.

For filtering on MasterGrid I use a DataView on Customers table. On
TextChange event of a textbox the MasterGrid shows only records matches
CustomerID with the textbox.

PROBLEM:
Now I click on any row of MasterGrid the DeatilGrid doesn’t show the related
detailed records automatically. Before using DataView on MasterGrid it was
OK. But when MasterGrid is associated with DataView for filtering, the
DeatailGrid doesn’t show related record.

I also tried with DataViewManager but I observe that DataViewManager filters
records only once after DataTable fill. After DataTable fill I try to filter
on other conditions but there is no filtering.

Please notify where I m doing mistake. A kind help required,
Arif.
Bart Mermuys - 17 Sep 2006 19:59 GMT
Hi,

> Hi all,
>
[quoted text clipped - 22 lines]
> OK. But when MasterGrid is associated with DataView for filtering, the
> DeatailGrid doesn't show related record.

You need to bind the DetailGrid to the Customers DataView too, eg. :

DataView customersDV = ....
...
MasterGrid.DataSource = customersDV;

DetailGrid.DataSource = customersDV;
DetailGrid.DataMember = "relCustomersOrders";

---
HTH,
Greetings

> I also tried with DataViewManager but I observe that DataViewManager
> filters
[quoted text clipped - 4 lines]
> Please notify where I m doing mistake. A kind help required,
> Arif.
Arif - 19 Sep 2006 11:53 GMT
Much thanks Bart, its working fine now.

Another thing:
I create data relation as follows:

Suppliers |---------< Products >----------| Categories

Suppliers and Products tables are attached with DataGrids while Categories
is attached with a ComboBox.

There are two master tables for Products table. I connected
Suppliers-Products using "relSuppliersProducts" and Categories-Products using
"relCategoriesProducts".

Suppliers datagrid is attached with Suppliers datatable while Products
datagrid with "relSuppliersProducts".

When I click on Products dataGrid the idSuppliers field is automatically
populated but idCategories field of Products is null.

If I use "relCategoriesProducts" as a DataMember for Products grid then the
association Suppliers-Products is broken.

Is there any way or I have to manually populate this idCategories field of
Products dataTable manually, maintaining the .

With thanks for your previous help,
Arif.

> Hi,
>
[quoted text clipped - 46 lines]
> > Please notify where I m doing mistake. A kind help required,
> > Arif.
Bart Mermuys - 19 Sep 2006 13:26 GMT
Hi,

> Much thanks Bart, its working fine now.
>
[quoted text clipped - 23 lines]
> Is there any way or I have to manually populate this idCategories field of
> Products dataTable manually, maintaining the .

Maybe but it's not entirely clear what you want.  There are two parents:
CategoryComboBox and SupplierDataGrid ...  What should happen to the
ProductDataGrid when the user changes one of the parents ?

Does the product table need to be filtered according to both Category and
Supplier ? Or some other behaviour ?

Greetings

> With thanks for your previous help,
> Arif.
[quoted text clipped - 50 lines]
>> > Please notify where I m doing mistake. A kind help required,
>> > Arif.
Arif - 19 Sep 2006 14:20 GMT
Yes Bart, the Product table should be filtered on both : the selected grid
Supplier and selected Category from combobox. Also new products can be
entered against the current Supplier and Category.

Currently the Products table is working only with Suppliers, not with
Categories. I refer the relationship from NWIND.mdb(sample).

I see that ADO.NET is good for one table or simple table relations but for
complex relation like here I may have to use custom sql queries.

Can you please recommend any good resource for ADO.NET to work with complex
table relationships?

Arif.

> Hi,
>
[quoted text clipped - 89 lines]
> >> > Please notify where I m doing mistake. A kind help required,
> >> > Arif.
Bart Mermuys - 19 Sep 2006 17:39 GMT
Hi,

> Yes Bart, the Product table should be filtered on both : the selected grid
> Supplier and selected Category from combobox. Also new products can be
[quoted text clipped - 5 lines]
> I see that ADO.NET is good for one table or simple table relations but for
> complex relation like here I may have to use custom sql queries.

Well, i see you have two options:

1) load the entire products table and do the filtering yourself using a
DataView:

private DataView categoryDV;
private DataView supplierDV;
private DataView productDV;
private Dataset ds = new Dataset();
// adapters...

private void Form1_Load(object sender, System.EventArgs e)
{
 // load all tables
 categoryAdapater.Fill(ds, "tbl_category");
 supplierAdapter.Fill(ds, "tbl_supplier");
 productAdapter.Fill(ds, "tbl_product");

 // create dataviews
 categoryDV = ds.Tables["tbl_category"].DefaultView;
 supplierDV = ds.Tables["tbl_supplier"].DefaultView;
 productDV = ds.Tables["tbl_product"].DefaultView;

 // bind
 catCb.DataSource = categoryDV;
 catCb.DisplayMember = "category";
 supDg.DataSource = supplierDV;
 prodDg.DataSource = productDV;

 UpdateProductFilter();

 // hook up category position changed (using CurrencyManager
 // returned from BindingContext).
 // warning: you must always use the same DataSource
 // (and DataMember) as the ones you used for binding
 BindingContext[categoryDV].PositionChanged+=
   new EventHandler(categoryCM_PositionChanged);

 // same for supplier
 BindingContext[supplierDV].PositionChanged+=
   new EventHandler(supplierCM_PositionChanged);

 // hook up listchanged to dedect new rows
 productDV.ListChanged+=
   new ListChangedEventHandler(productDV_ListChanged);
}

private void categoryCM_PositionChanged(object sender, EventArgs e)
{
 UpdateProductFilter();
}

private void supplierCM_PositionChanged(object sender, EventArgs e)
{
 UpdateProductFilter();
}

private void UpdateProductFilter()
{
 int catID = (int) // get current category_id
   ((DataRowView)BindingContext[categoryDV].Current)["ID"];

 int supID = (int) // get current supplier_id
   ((DataRowView)BindingContext[supplierDV].Current)["ID"];

 productDV.RowFilter =
    string.Format("(category_id = {0}) AND (supplier_id = {1})",
       catID, supID);
}

private void productDV_ListChanged(object sender, ListChangedEventArgs e)
{
 int catID = (int)
    ((DataRowView)BindingContext[categoryDV].Current)["ID"];

 int supID = (int)
    ((DataRowView)BindingContext[supplierDV].Current)["ID"];

 if ( (e.ListChangedType == ListChangedType.ItemAdded) &&
   (productDV[e.NewIndex].IsNew) )
 {
    // set the foreign keys
    productDV[e.NewIndex]["category_id"] = catID;
    productDV[e.NewIndex]["supplier_id"] = supID;
 }
}

OR (2) each time category or supplier changes you load only the revelant
rows using a DataAdapter with a parameterized select query.   This would
require a DataAdapter which has a select query with a parameter in it ,eg:
"SELECT * FROM tbl_products WHERE category_id = ? AND supplier_id = ?"

If you do this using the designer it will add the right Parameters to the
(Select)Command' Parameter collection also, if you do it from code, you need
to add the parameters youself:
see productDataAdapter.SelectCommand.Parameters.Add(...)

Once you have setup the right DataAdapter, you can then change the code
within UpdateProductFilter:

private void UpdateProductFilter()
{
 int catID = (int) // get current category_id
   ((DataRowView)BindingContext[categoryDV].Current)["ID"];

 int supID = (int) // get current supplier_id
   ((DataRowView)BindingContext[supplierDV].Current)["ID"];

 productAdapter.SelectCommand.Parameters("category_id").Value = catID;
 productAdapter.SelectCommand.Parameters("supplier_id").Value = supID;

 ds.Tables["tbl_product"].Clear();
 productAdapter.Fill(ds, "tbl_product");
}

> Can you please recommend any good resource for ADO.NET to work with
> complex
> table relationships?

Can't think of any now but parts can definitely be found on google, msdn or
codeproject....

HTH,
Greetings

> Arif.
>
[quoted text clipped - 99 lines]
>> >> > Please notify where I m doing mistake. A kind help required,
>> >> > Arif.
Arif - 20 Sep 2006 07:18 GMT
Much much thanks dear Bart for your detailed help. I got much idea with this.

With thanks again,
Arif.

> Hi,
>
[quoted text clipped - 236 lines]
> >> >> > Please notify where I m doing mistake. A kind help required,
> >> >> > Arif.
Bart Mermuys - 17 Sep 2006 19:59 GMT
Hi,

> Hi all,
>
[quoted text clipped - 22 lines]
> OK. But when MasterGrid is associated with DataView for filtering, the
> DeatailGrid doesn't show related record.

You need to bind the DetailGrid to the Customers DataView too, eg. :

DataView customersDV = ....
...
MasterGrid.DataSource = customersDV;

DetailGrid.DataSource = customersDV;
DetailGrid.DataMember = "relCustomersOrders";

---
HTH,
Greetings

> I also tried with DataViewManager but I observe that DataViewManager
> filters
[quoted text clipped - 4 lines]
> Please notify where I m doing mistake. A kind help required,
> Arif.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.