Walk thru all relations in dataset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    Walk thru all relations in dataset

    Hi

    I have a dataset with 3 tables and 2 relations
    Is there a way to when I am at 1 row to tell if there is a relation on that
    row ???
    I have the code hardcoded but try to make it work if the # of tables and
    #relations increase or decrease
    So I can just pass any dataset and walk thru the rows??

    Thanks

    for (int p = 0; p < ds.Tables[1].Rows.Count; p++)

    {

    //Get Childs rows of parent

    DataRow [] rows = ds.Tables[1].Rows[p].GetChildRows(" Level1");

    for ( int t = 0; t < rows.Length; t++)

    {

    DataRow drc = rows[t]; //Current row


    //Get Childs rows of parent

    DataRow [] childrows = drc.GetChildRow s("Level2");

    }

    }


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Walk thru all relations in dataset

    Joe,

    What I would do is before you loop through the rows in a table, cycle
    through the relations to find out which one uses your table as the parent
    table. Store those in a list.

    Then, through each row, you can find the child rows by calling
    GetChildRows on each row for the relation for each relation you know your
    current table is a parent of.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "Joe" <hchs1977@yahoo .com> wrote in message
    news:e4uXuQ2VGH A.2760@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hi
    >
    > I have a dataset with 3 tables and 2 relations
    > Is there a way to when I am at 1 row to tell if there is a relation on
    > that row ???
    > I have the code hardcoded but try to make it work if the # of tables and
    > #relations increase or decrease
    > So I can just pass any dataset and walk thru the rows??
    >
    > Thanks
    >
    > for (int p = 0; p < ds.Tables[1].Rows.Count; p++)
    >
    > {
    >
    > //Get Childs rows of parent
    >
    > DataRow [] rows = ds.Tables[1].Rows[p].GetChildRows(" Level1");
    >
    > for ( int t = 0; t < rows.Length; t++)
    >
    > {
    >
    > DataRow drc = rows[t]; //Current row
    >
    >
    > //Get Childs rows of parent
    >
    > DataRow [] childrows = drc.GetChildRow s("Level2");
    >
    > }
    >
    > }
    >
    >[/color]


    Comment

    • Joe

      #3
      Re: Walk thru all relations in dataset

      Hi Nicholas

      Thanks

      The problem is that the relation is at different levels
      This is a dataset with 3 tables

      table1 shows 2 relations l1 and l2
      From Table1 I get a row collection from the parent row with relation
      From 1 row from collection I get another collectiom with another relation

      The Tables are all cascading
      I am starting at the top level maybe I should start at the bottom???

      I am submitting another question to the group because I am not sure if this
      is easier or
      xml and xpath if I use the xsd - this dataset can have any number of fields
      and relations
      and I get it down stream

      Thanks


      DataRow [] rows = ds.Tables[1].Rows[p].GetChildRows(" l1");
      for ( int t = 0; t < rows.Length; t++)

      {

      DataRow drc = rows[t]; //Current row

      DataRow [] childrows = drc.GetChildRow s("l2");



      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:e0oGap2VGH A.5692@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Joe,
      >
      > What I would do is before you loop through the rows in a table, cycle
      > through the relations to find out which one uses your table as the parent
      > table. Store those in a list.
      >
      > Then, through each row, you can find the child rows by calling
      > GetChildRows on each row for the relation for each relation you know your
      > current table is a parent of.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >
      > "Joe" <hchs1977@yahoo .com> wrote in message
      > news:e4uXuQ2VGH A.2760@TK2MSFTN GP11.phx.gbl...[color=green]
      >> Hi
      >>
      >> I have a dataset with 3 tables and 2 relations
      >> Is there a way to when I am at 1 row to tell if there is a relation on
      >> that row ???
      >> I have the code hardcoded but try to make it work if the # of tables and
      >> #relations increase or decrease
      >> So I can just pass any dataset and walk thru the rows??
      >>
      >> Thanks
      >>
      >> for (int p = 0; p < ds.Tables[1].Rows.Count; p++)
      >>
      >> {
      >>
      >> //Get Childs rows of parent
      >>
      >> DataRow [] rows = ds.Tables[1].Rows[p].GetChildRows(" Level1");
      >>
      >> for ( int t = 0; t < rows.Length; t++)
      >>
      >> {
      >>
      >> DataRow drc = rows[t]; //Current row
      >>
      >>
      >> //Get Childs rows of parent
      >>
      >> DataRow [] childrows = drc.GetChildRow s("Level2");
      >>
      >> }
      >>
      >> }
      >>
      >>[/color]
      >
      >[/color]


      Comment

      Working...