none
Mapping two DataSet's RRS feed

  • Question

  • In my application, I have one DataSet with all of my "raw" information, which is difficult to read and edit.  I would like to create a second DataSet that contains a more user friendly version of the data.  For example, in my original DataSet I have a column called "foo" which stores an integer.  This integer is actually an encoded value that maps to a string.

    In my new DataSet I would like to have a "foo" column of type string.  The user will enter the string in the DataGridView, which will then update the original DataSet with the encoded integer value.  Conversely, if the user were to update the original DataSet integer, the new DataSet will then be updated with the corresponding string.

    How can I achieve this functionality?  It seems like it's possible, but I'm not sure where to start.  Will DataAdapter be able to help me?  What about TableAdapter?

    Thanks for any help.
    Monday, September 14, 2009 6:50 PM

Answers

  • Hi BigHands,

     

    You can use Datatable.Merge to merge two table with similar schema as long as the data is compatible in both table.

    For example, you can do like this:

                DataSet ds = new DataSet();

               

                DataTable dt1 = new DataTable();

                dt1.Columns.Add(new DataColumn("Date", System.Type.GetType("System.String")));

     

                DataTable dt2 = new DataTable();

                dt2.Columns.Add(new DataColumn("Date", System.Type.GetType("System.Int32")));

     

                dt1.Rows.Add("123");

                dt2.Merge(dt1, false, MissingSchemaAction.Ignore);

     

    However, you can’t use dt1.Rows.Add("abc") . It is incompatible to int type and will cause exception. For more information about Merge, you can refer to MSDN document:

    http://msdn.microsoft.com/en-us/library/wtk78t63.aspx

     

     

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    • Marked as answer by Yichun_Feng Monday, September 21, 2009 2:31 AM
    Wednesday, September 16, 2009 8:48 AM

All replies

  • Continuing to look into my issue, I believe I phrased my initial question wrong.  I have one DataSet with two DataTables; each with a different schema.  For example, in Table A, "date" is stored as an integer and in Table B, "date" is stored as a string.  How can I easily populate Table A from Table B without going through a "foreach row" loop?

    Thanks!
    Tuesday, September 15, 2009 7:23 PM
  • Hi BigHands,

     

    You can use Datatable.Merge to merge two table with similar schema as long as the data is compatible in both table.

    For example, you can do like this:

                DataSet ds = new DataSet();

               

                DataTable dt1 = new DataTable();

                dt1.Columns.Add(new DataColumn("Date", System.Type.GetType("System.String")));

     

                DataTable dt2 = new DataTable();

                dt2.Columns.Add(new DataColumn("Date", System.Type.GetType("System.Int32")));

     

                dt1.Rows.Add("123");

                dt2.Merge(dt1, false, MissingSchemaAction.Ignore);

     

    However, you can’t use dt1.Rows.Add("abc") . It is incompatible to int type and will cause exception. For more information about Merge, you can refer to MSDN document:

    http://msdn.microsoft.com/en-us/library/wtk78t63.aspx

     

     

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    • Marked as answer by Yichun_Feng Monday, September 21, 2009 2:31 AM
    Wednesday, September 16, 2009 8:48 AM