Solved

copying dataview rows to datatable

Posted on 2004-03-31
7
471 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 75 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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