Solved

Many-to-Many DATARELATION :Couldnt' figure it at all

Posted on 2004-09-10
5
222 Views
Last Modified: 2010-04-15
hi,,


I have 4 tables. t1, t2, t3, t4.

t2 and t4 are many to many.

DATAGRID 1:
t1 - parent table - Primarykey (t1-ID)
    - t2 child table -Primary key (t2-id), Foreign Key (t1-ID), column (t3-ID)not unique, and more columns

DATAGRID 2:

t3 - parent table - Primarykey (t3-ID)
    - t4 child table -Primary key (t4-id), Foreign Key (t3-ID) not unique, and more columns

DATAGRID 3 :

 I need to display the corresponding records in t4 for selection of  't2' in datagrid 1..

As u can see the link for the two tables 't2' and 't4'  is  "t3-ID"...


How to do this?
0
Comment
Question by:Jzzzz
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12027540
One way to do this is to capture the CurrentCellChanged event on DataGrid1 if the CurrentRow is different then previously, then get the t3-id and loop through table 4 getting all records that have a mathcing t3-id and add those records to DataGrid3...
0
 

Author Comment

by:Jzzzz
ID: 12028131
pls can u show me in code...what will happen in

mydatagrid_currentcellchanged(sender, e)
{
  //   ? mydatagrid.currenrowIndex ?

   //how to check if current row is different
   // how to get the column field i.e t3-ID
  // how to loop through the table4

 
  // mydatagrid3.bind();
}
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 250 total points
ID: 12028449
It should look something like:

private int lastRowIndex = 0;
private int t3IdColIndex = 3;

mydatagrid_currentcellchanged(sender, e)
{
   if ( mydatagrid.currentRowIndex != lastRowIndex ) {

      lastRowIndex = mydatagrid.currentRowIndex;
      string id = mydatagrid[ lastRowIndex, t3IdColIndex ].ToString();

      DataTable t5 = new DataTable();
      foreach ( DataRow dr in t4 ) {
         if ( dr["t3-ID"].ToString().Equals(id) ) {
            t5.Rowd.Add( dr );
         }
      }
      mydatagrid3.DataSource = t5;
   }
}
0
 

Author Comment

by:Jzzzz
ID: 12028807

before the 'foreach' statement
i did this

DataTable dt_new = new DataTable();
         
     dt_new  = mydataSet1.t4 ;

and then used that in the 'foreach'

-----  foreach ( DataRow dr in  dt_new ) {  createrows and bindgrid} ------

but its giving me this error
--------------------------------------------------------------------------------------------------------
"foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System.Data.DataTable' does not contain a definition for 'GetEnumerator', or it is inaccessible"
--------------------------------------------------------------------------------------------------------


and Should  this "private int lastRowIndex = 0;"
 be  "static int lastRowIndex;"


0
 

Author Comment

by:Jzzzz
ID: 12030293
oh..

it has to be

foreach ( DataRow dr in  dt_new.Rows)..

---> dt_new.Rows

And when i run the program

it gives an exception : System.Argument exception at the "foreach( DataRow dr in  dt_new.Rows).."

additional information : "this row is already in another table"

Question : WE are trying to get the rows from a different table in the dataset using the DataRow object., when the currentcellchanged event is fired. is that possible?



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now