Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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