Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add One DataView to Another in C#

I need to add two DataViews together to have one Dataview that can then be bound to a Repeater.

I am plugging into someone else's API so I can't change the way the data is retreived at the SQL Level.

So essentially I want to do this:

DataView dView1 = getActiveModules();
DataView dView2 = getInactiveModules();

ModuleView = dView1 + dView2;

rptModules.DataSource = ModuleView.Tables[0];
rptModules.DataBind();

The two schemas for the views are identical just retrieving active and inactive modules.

Any ideas?

Thanks.

like image 469
Seth Duncan Avatar asked Jan 21 '23 21:01

Seth Duncan


2 Answers

you can merge your dataview like...

System.Data.DataView dv = new System.Data.DataView();
System.Data.DataView dv1 = new System.Data.DataView();
dv.Table.Merge(dv1.Table);
like image 155
Muhammad Akhtar Avatar answered Jan 25 '23 15:01

Muhammad Akhtar


You can easily combine/merge the two views (data tables) into one data table.

Example Syntax

    Dim a As DataView
    Dim b As DataView

    a.Table.Merge(b.Table)

    Dim c As New DataView
    c.Table.Merge(a.Table) 'might generate error because c.Table is null

From http://msdn.microsoft.com/en-us/library/system.data.datatable.merge.aspx

DataTable.Merge Method Merge the specified DataTable with the current DataTable.

The Merge method is used to merge two DataTable objects that have largely similar schemas. A merge is typically used on a client application to incorporate the latest changes from a data source into an existing DataTable. This allows the client application to have a refreshed DataTable with the latest data from the data source.

The merge operation takes into account only the original table, and the table to be merged. Child tables are not affected or included. If a table has one or more child tables, defined as part of a relationship, each child table must be merged individually.

like image 36
AMissico Avatar answered Jan 25 '23 15:01

AMissico