Casting generic datatable to typed datatable
Asked Answered
V

4

17

I need to reuse a DataAccess method prescribed by client. This method returns a vanilla datatable. I want to cast this datatable to my Typed datatable. The amount of columns and their types will match. The exception message "Unable to cast object of type 'System.Data.DataTable' to type 'MarketValueDataTable'." is very clear, but how do I fix it?

Had a look at casting-a-base-type-to-a-derived-type, but could not see how to make that work.

I cannot populate the datatable with a datareader, can only use the client's DataAccess method.

Volume answered 9/9, 2009 at 9:43 Comment(0)
I
37

The cast could only work if the table returned by the method was actually an instance of MarketValueDataTable. If it's not, all you can do is copy the data to an instance of MarketValueDataTable. You could for instance use the Merge method :

DataTable data = GetData();
MarketValueDataTable myData = new MarketValueDataTable();
myData.Merge(data);
Insignificance answered 9/9, 2009 at 10:5 Comment(1)
What are the costs of merging?!Manichaeism
P
8

Use this handy function to convert a regular DataTable to a typed DataTable.

VB

Public Shared Function ConvertToTypedDataTable(Of T As {Data.DataTable, New})(dtBase As Data.DataTable) As T
    Dim dtTyped As New T
    dtTyped.Merge(dtBase)

    Return dtTyped
End Function

Usage

Dim dtTyped = ConvertToTypedDataTable(Of dataset1.MyTypedDataTable)(MyUntypedDataTable)

C#

public static T ConvertToTypedDataTable<T>(System.Data.DataTable dtBase) where T : Data.DataTable, new()
{
    T dtTyped = new T();
    dtTyped.Merge(dtBase);

    return dtTyped;
}

Usage

dataset1.MyTypedDataTable dtTyped = ConvertToTypedDataTable<dataset1.MyTypedDataTable>(MyUntypedDataTable);
Piapiacenza answered 31/10, 2012 at 20:51 Comment(0)
C
1

You cannot cast the DataTable to a MarketValueDataTable, for the simple reason that it doesn't inherit from it.

What you need to do is instantiate a new MarketValueDataTable, then copy the data from the DataTable into it.

Try something like:

MarketValueDataTable myTable = new MarketValueDataTable();
StringWriter writer = new StringWriter();
theDataTable.WriteXml(writer);
myTable.ReadXml(new StringReader(writer.ToString()));

(I haven't tested this)

Cleek answered 9/9, 2009 at 10:2 Comment(0)
S
0

If your datatable is a generic table then it can't be cast to a MarketValueDataTable.

One thing you can try is to create a new MarketValueDataTable object and manually add rows to it. By stepping through the rows of your generic datatable and then stepping through the columns of each row you could use reflection to set the property values of a new MarketValueDataTableRow.

Something like the following (warning - pseudocode):

MarketValueDataTable mv = new MarketValueDataTable();
foreach(DataRow row in table.Rows)
{

   MarketValueDataTableRow mvrow = mv.NewRow();
   foreach(DataColumn col in table.Columns)
   {
      PropertyInfo colProperty = mvrow.GetType().GetProperty(col.Name);
      colProperty.SetValue(mvRow, row[col]);
   }
   mv.Rows.Add(mvrow);

}

You get the general idea. Step through the rows of the generic table and use reflection to find the matching properties in the specific typed data row. This should work (I have used the same approach earlier), but the code must be modified to match your requirements.

Sherrisherrie answered 9/9, 2009 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.