Solved

copying dataview rows to datatable

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 45
Different Delete Messages 7 37
Iterate a dictionnary to change values 4 53
Authentication of Web Services 3 46
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

930 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

18 Experts available now in Live!

Get 1:1 Help Now