Solved

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

Posted on 2004-09-10
5
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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