?
Solved

copying dataview rows to datatable

Posted on 2004-03-31
7
Medium Priority
?
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 10727023
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
ID: 10732476
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
ID: 10748889
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
ID: 10748894
Sorry, you probably realize this, but my example shoud read:

   if(ROW_MEETS_CRITERIA)
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 225 total points
ID: 10748901
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 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