Cast DataView as a DataTable

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

    Cast DataView as a DataTable

    I'm using a third-party tool that takes a DataTable as a parameter. I really
    need to pass it a DataView instead. When I try to explicitly cast the
    DataView as a DataTable I get an error that they aren't compatible data
    types. Does anyone have a suggestion on how to do this?

    My last resort is to create a new DataTable object and copy the rows from
    the DataView into the DataTable. But this is an ugly hack and I really hope
    there is a better solution.

    Thanks.


  • Mark Fitzpatrick

    #2
    Re: Cast DataView as a DataTable

    Can't do it. You can access the table that is part of the DataView using the
    DataView.Table property, but otherwise they're two way different beasties so
    you can't cast them.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP- FrontPage

    "Brian Bischof" <brian@nospam.b ischofsystems.c om> wrote in message
    news:emA2PA83DH A.540@tk2msftng p13.phx.gbl...[color=blue]
    > I'm using a third-party tool that takes a DataTable as a parameter. I[/color]
    really[color=blue]
    > need to pass it a DataView instead. When I try to explicitly cast the
    > DataView as a DataTable I get an error that they aren't compatible data
    > types. Does anyone have a suggestion on how to do this?
    >
    > My last resort is to create a new DataTable object and copy the rows from
    > the DataView into the DataTable. But this is an ugly hack and I really[/color]
    hope[color=blue]
    > there is a better solution.
    >
    > Thanks.
    >
    >[/color]


    Comment

    • Brian Bischof

      #3
      Re: Cast DataView as a DataTable


      Thanks for the info. I wrote a method to create a new table based off the
      original table structure and then add the rows into it. Lots of loops!

      Brian



      "Mark Fitzpatrick" <markfitz@fitzm e.com> wrote in message
      news:O9qfJF83DH A.560@TK2MSFTNG P11.phx.gbl...[color=blue]
      > Can't do it. You can access the table that is part of the DataView using[/color]
      the[color=blue]
      > DataView.Table property, but otherwise they're two way different beasties[/color]
      so[color=blue]
      > you can't cast them.
      >
      > Hope this helps,
      > Mark Fitzpatrick
      > Microsoft MVP- FrontPage
      >
      > "Brian Bischof" <brian@nospam.b ischofsystems.c om> wrote in message
      > news:emA2PA83DH A.540@tk2msftng p13.phx.gbl...[color=green]
      > > I'm using a third-party tool that takes a DataTable as a parameter. I[/color]
      > really[color=green]
      > > need to pass it a DataView instead. When I try to explicitly cast the
      > > DataView as a DataTable I get an error that they aren't compatible data
      > > types. Does anyone have a suggestion on how to do this?
      > >
      > > My last resort is to create a new DataTable object and copy the rows[/color][/color]
      from[color=blue][color=green]
      > > the DataView into the DataTable. But this is an ugly hack and I really[/color]
      > hope[color=green]
      > > there is a better solution.
      > >
      > > Thanks.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Cast DataView as a DataTable

        Brian,
        You do realize that you can use DataTable.Clone to "create a new table based
        off the original table structure"?

        Then you only need a single loop to add the respective rows to the new
        DataTable.

        Unless of course you are modifying the structure itself of the original
        DataTable.

        BTW: DataTable.Clone will copy the structure of an existing DataTable, while
        DataTable.Copy will copy the structure & data of an existing DataTable.

        Hope this helps
        Jay

        "Brian Bischof" <brian@nospam.b ischofsystems.c om> wrote in message
        news:uhW1Ly83DH A.3944@tk2msftn gp13.phx.gbl...[color=blue]
        >
        > Thanks for the info. I wrote a method to create a new table based off the
        > original table structure and then add the rows into it. Lots of loops!
        >
        > Brian
        >
        >
        >
        > "Mark Fitzpatrick" <markfitz@fitzm e.com> wrote in message
        > news:O9qfJF83DH A.560@TK2MSFTNG P11.phx.gbl...[color=green]
        > > Can't do it. You can access the table that is part of the DataView using[/color]
        > the[color=green]
        > > DataView.Table property, but otherwise they're two way different[/color][/color]
        beasties[color=blue]
        > so[color=green]
        > > you can't cast them.
        > >
        > > Hope this helps,
        > > Mark Fitzpatrick
        > > Microsoft MVP- FrontPage
        > >
        > > "Brian Bischof" <brian@nospam.b ischofsystems.c om> wrote in message
        > > news:emA2PA83DH A.540@tk2msftng p13.phx.gbl...[color=darkred]
        > > > I'm using a third-party tool that takes a DataTable as a parameter. I[/color]
        > > really[color=darkred]
        > > > need to pass it a DataView instead. When I try to explicitly cast the
        > > > DataView as a DataTable I get an error that they aren't compatible[/color][/color][/color]
        data[color=blue][color=green][color=darkred]
        > > > types. Does anyone have a suggestion on how to do this?
        > > >
        > > > My last resort is to create a new DataTable object and copy the rows[/color][/color]
        > from[color=green][color=darkred]
        > > > the DataView into the DataTable. But this is an ugly hack and I really[/color]
        > > hope[color=darkred]
        > > > there is a better solution.
        > > >
        > > > Thanks.
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...