How to create a datatable object from a datarow [].

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

    #1

    How to create a datatable object from a datarow [].


    I have an array of datarow . Is there a performance efficient way of
    deriving a datatable oject
    containing only the rows in my array. ?

    I want to avoid uding foreach .

    Many thanks

    JB


  • bfking

    #2
    Re: How to create a datatable object from a datarow [].

    where did you get the array of datarows?? the only way i know of is
    looping through them and using dt.Rows.Add(... ); if there's a better
    way, i'd also like to know...

    Comment

    • jensen bredal

      #3
      Re: How to create a datatable object from a datarow [].

      if there's a better[color=blue]
      > way, i'd also like to know...
      >[/color]

      Here is the code:

      DataSet ds=GetDataSet() ;


      DataTable dt=ds.Tables[0];

      int i=5, j=11;

      if(dt.Rows.Coun t-i>0)

      {

      DataRow[] selectedRows=ne w DataRow[(j-i)];

      for(int index=0; index<selectedR ows.Length; index++)

      {

      selectedRows[index]=dt.Rows[i+index];

      }


      return selectedRows;


      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: How to create a datatable object from a datarow [].

        Hi,

        What about using a DataView ?

        Cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation



        "jensen bredal" <jensen.bredal@ yahoo.dk> wrote in message
        news:e5VeZIxTFH A.2392@TK2MSFTN GP10.phx.gbl...[color=blue]
        > if there's a better[color=green]
        >> way, i'd also like to know...
        >>[/color]
        >
        > Here is the code:
        >
        > DataSet ds=GetDataSet() ;
        >
        >
        > DataTable dt=ds.Tables[0];
        >
        > int i=5, j=11;
        >
        > if(dt.Rows.Coun t-i>0)
        >
        > {
        >
        > DataRow[] selectedRows=ne w DataRow[(j-i)];
        >
        > for(int index=0; index<selectedR ows.Length; index++)
        >
        > {
        >
        > selectedRows[index]=dt.Rows[i+index];
        >
        > }
        >
        >
        > return selectedRows;
        >
        >[/color]


        Comment

        • jensen bredal

          #5
          Re: How to create a datatable object from a datarow [].


          "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
          in message news:uqEyFqxTFH A.580@TK2MSFTNG P15.phx.gbl...[color=blue]
          > Hi,
          >
          > What about using a DataView ?
          >[/color]

          Could you please explain based on the code posted earlier?

          JB


          Comment

          • James Curran

            #6
            Re: How to create a datatable object from a datarow [].

            Basically, it would be along the lines of:

            DataSet ds=GetDataSet() ;
            DataTable dt=ds.Tables[0];
            DataView dv = new DataView(dt, "filter expression", "sort expression",
            DataViewRowStat e.CurrentRows)
            return dv;

            where "filter expression" would be exactly as you'd write it in a WHERE
            clause, and sort expression just as you'd write it in a ORDER BY.

            Presumably, you have a better way of expressing the subset you want to
            filter on, than "rows 5 thru 11"/
            --
            --
            Truth,
            James Curran
            [erstwhile VC++ MVP]

            Home: www.noveltheory.com Work: www.njtheater.com
            Blog: www.honestillusion.com Day Job: www.partsearch.com

            "jensen bredal" <jensen.bredal@ yahoo.dk> wrote in message
            news:ujGrWyxTFH A.2420@TK2MSFTN GP12.phx.gbl...[color=blue]
            >
            > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
            wrote[color=blue]
            > in message news:uqEyFqxTFH A.580@TK2MSFTNG P15.phx.gbl...[color=green]
            > > Hi,
            > >
            > > What about using a DataView ?
            > >[/color]
            >
            > Could you please explain based on the code posted earlier?
            >
            > JB
            >
            >[/color]


            Comment

            • jensen bredal

              #7
              Re: How to create a datatable object from a datarow [].

              >[color=blue]
              > Presumably, you have a better way of expressing the subset you want to
              > filter on, than "rows 5 thru 11"/
              > --[/color]

              That is the problem. I see no obvious way of doing this through a sort
              expression.

              5 through 11 is in fact almost what i'm trying to do. I have a datatable
              with n rows.

              each time i only wnat to subtract a subset and the nex subset the next time
              and so on.
              I see no obvious way to this with sql.
              Do you?
              JB


              Comment

              Working...