Search code examples
vb.netlistdataview

How can I merge 2 DataView built in 2 different way?


I am not a c# developer and I found myself at work working with a huge C# application...hug..

Now I am trying to populate an existing DataGrid with some more data which has been 'created' in a different way...let me show you what I mean.

This is how the existing DataView is generated:

Dim lDataSet As System.Data.DataSet
Dim lSqlCommand As New System.Data.SqlClient.SqlCommand
Dim lConvert As New PeoplePlanner.Common.Data.Convert

lSqlCommand.CommandType = CommandType.StoredProcedure
lsqlCommand.CommandText = "AccessIntegrationEmployeeInboundSearch"

lDataSet = objDatabase.GetDataSet(lSqlCommand)

If Not IsNothing(lDataSet) Then
    objDataView = lDataSet.Tables(0).DefaultView
End If

There is probably be stuff missing in that code but hopefully it is enough to get an overview. It uses Stored Procedure (SP) to get some data from the DB and it returns the result inside 'lDataSet' and then we do 'objDataView = lDataSet.Tables(0).DefaultView'.

Now the new data which I want to 'merge in' it has the same fields but it is not the response of a SP but of an operation I have created which returns back a List of OnBoardingEmployee(let's call it like that) and if I want to correctly show ONLY the data inside that list this is what I do:

 Dim lDataView As System.Data.DataView

 Dim op = CoreInjector.Inject(Of IGetAllDataHubIncomingMessagesToBeProcess).Execute()
 lDataView = Common.Detail.DataGridOperationHelper.ConvertToDataTable(op.OnBoardingEmployee).DefaultView

 objDataView = lDataView

Where:

  • op contains a list of OnBoardingEmployee which has the same fields as the existing table
  • lDataView basically is the same as objDataView that's why as last step I assign objDataView = lDataView

Now I would basically like to merge, join, add, whatever The objDataView created for the first table: objDataView = lDataSet.Tables(0).DefaultView

with the one I have created afterwards:

 lDataView = Common.Detail.DataGridOperationHelper.ConvertToDataTable(x.OnBoardingEmployee).DefaultView

How do I do that? :'(

They have the same data structure or better say they use the same view.

Thanks for the help :)


Solution

  • I have got it working :) this is my implementation:

            Dim employeeJSONTable As DataTable
            Dim employeeExistingTable As DataTable
            Dim myDataRow As DataRow
            Dim employeeID As Integer
    
            if Request.QueryString.Get("Function") = "HubInbound" then
                Dim employeeJSONList = CoreInjector.Inject(Of IGetAllDataHubIncomingMessagesToBeProcess).Execute()
                employeeJSONTable = Common.Detail.DataGridOperationHelper.ConvertToDataTable(employeeJSONList.OnBoardingEmployee)
                employeeJSONTable.PrimaryKey = New DataColumn(){ employeeJSONTable.Columns("EmployeeID")}
            End If
    
            lDataSet = objDatabase.GetDataSet(lSqlCommand)
            employeeExistingTable = lDataSet.Tables(0)
    
            For Each row As DataRow In employeeExistingTable.Rows
    
                employeeID = row.Item("EmployeeID")
    
                myDataRow = employeeJSONTable.Rows.Find(employeeID)
    
                if (myDataRow Is Nothing) then
                    employeeJSONTable.ImportRow(row)
                End If
    
            Next row
    
            If Not IsNothing(employeeJSONTable.DefaultView) And Request.QueryString.Get("Function") = "HubInbound" Then
                    objDataView = employeeJSONTable.DefaultView
            Elseif Not IsNothing(lDataSet) Then
                    objDataView = lDataSet.Tables(0).DefaultView
            End if