Solved

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

Posted on 2004-09-10
5
223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

864 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

21 Experts available now in Live!

Get 1:1 Help Now