Solved

copying dataview rows to datatable

Posted on 2004-03-31
7
463 Views
Last Modified: 2010-05-18
I have a dataset with over 5000 rows of data so I use a dataview to filter the data to about 10 rows.  Now I need to display these 10 rows as XML.  Since the dataview dosn't have a method to display the data in XML, I was thinking of doing the following:

Create a datatable that's an exact replica (without the data) of an existing datatable in the dataset:

DataTable DataTable_Model = DataSet_AddrRec.Tables[0];
DataTable DataTable_filter = DataSet_AddrRec.Tables.Add("TableFilter");
int iColumnCount = DataTable_Model.Columns.Count;
for (int i=0; i < iColumnCount; i++)
{
      DataTable_filter.Columns.Add(DataTable_Model.Columns[i].ToString());
}

and once I have this new datatable, I'd like to copy the rows in the dataview to this new datatable.
Is this possible? The problem I'm having is that I don't know how to extract data from the view. This is also assuming that I can write the data in a datatable as XML.

Any help would be appreciated,
Vaughn
0
Comment
Question by:MyersA
  • 4
7 Comments
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
To copy the rows to the second table:

  foreach(System.Data.DataRow sourceRow in DataTable_Model.Rows)
  {
    if(ROW_MEETS_CRITERIA)
    {
      DataTable_filter.Rows.Add(sourceRow.ItemArray);
    }
  }
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
Thanks for your reply.

So I can't browse through the dataview and copy them directly to the second table?
The reason I'm asking is because I already did the filter, but in your example, I'd be doing the filter again.
So I'd be filtering the 5000+ records to create the dataview, and then I'd have to go through the 5000+ records again in order to fill the 2nd table.

Vaughn
0
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
I see know reason why you couldn't do this (replace this.dataView1 with your DataView object):

  foreach(System.Data.DataRow sourceRow in this.dataView1.Table.Rows)
  {
    if(true)//ROW_MEETS_CRITERIA)
    {
      DataTable_filter.Rows.Add(sourceRow.ItemArray);
    }
  }


Or, you could use DataTable.Select to produce a DataRow[] array, then you can assign this array to the DataView, and the second table at the same time.
0
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
Sorry, you probably realize this, but my example shoud read:

   if(ROW_MEETS_CRITERIA)
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 75 total points
Comment Utility
Actually, NO NO NO, we don't need to check the criteria, because we know the *entire* dataview is okay.  So the code is simply,

  foreach(System.Data.DataRow sourceRow in this.dataView1.Table.Rows)
    DataTable_filter.Rows.Add(sourceRow.ItemArray);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Response back from stored procedure 6 47
Pass through dll 2 34
Data Saving 5 22
Get String split 5 31
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now