Solved

copying dataview rows to datatable

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

Independent Software Vendors: 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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

734 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