locked
Is it possible to bind two objectdatasource controls to one control(Like grid view,Table ) RRS feed

  • Question

  • User-2089467002 posted

    Hi,

    Say i have two different tables with different columns.
    I am selecting columnn names from two different tables into two different Objectdatasource controls using select method.
    Is it possible to bind the data from two objectdatasorces into one.

    Tuesday, July 9, 2013 6:44 AM

Answers

  • User-1219482859 posted

    Hi,

    yes you can merge datatables,dataset and datarow array with different columns. Please check the below link:

    http://msdn.microsoft.com/en-us/library/aszytsd8(v=vs.80).aspx

    otherwise you can use below code to implement your column merge

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Create the dataset and put in the data. Normally you will just get this from a database query'

    Dim ds1 As New Data.DataSet
    Dim ds2 As New Data.DataSet

    Dim dt1 = ds1.Tables.Add()
    dt1.Columns.Add("ID", GetType(Int32))
    dt1.Columns.Add("ColA", GetType(String))
    dt1.Columns.Add("ColB", GetType(String))

    Dim dt2 = ds2.Tables.Add()
    dt2.Columns.Add("ColC", GetType(String))
    dt2.Columns.Add("ColD", GetType(String))

    dt1.Rows.Add(1, "a", "b")
    dt2.Rows.Add("c", "d")
    dt1.Rows.Add(2, "e", "f")
    dt2.Rows.Add("g", "h")

    'Sample data created, now to merge the results like you want'
    Dim dsNew As New Data.DataSet
    Dim dtNew = dsNew.Tables.Add(0)

    MergeColumns(dtNew, dt1, dt2)
    MergeData(dtNew, dt1, dt2)

    'Display the results'
    dsNew.AcceptChanges()
    Response.Write(dsNew.GetXml)

    End Sub


    Private Sub MergeColumns(ByVal TargetTable As Data.DataTable, ByVal ParamArray SourceTables() As Data.DataTable)
    For Each dtSource In SourceTables
    'Make a clone of the table, then steal the columns from the clone'
    Dim dtClone = dtSource.Clone

    While dtClone.Columns.Count > 0
    Dim dc = dtClone.Columns(0)

    dtClone.Columns.Remove(dc)

    TargetTable.Columns.Add(dc)
    End While
    Next
    End Sub


    Private Sub MergeData(ByVal TargetTable As Data.DataTable, ByVal ParamArray SourceTables() As Data.DataTable)
    'Determine the number of rows the final table will have'
    Dim nMaxRowCount = 0
    For Each dt In SourceTables
    If dt.Rows.Count > nMaxRowCount Then
    nMaxRowCount = dt.Rows.Count
    End If
    Next

    For i = 0 To nMaxRowCount - 1
    'Create a new row using column data from each table. Assumes the name is unique across tables.'
    Dim drTarget = TargetTable.NewRow
    For Each dcTarget As Data.DataColumn In TargetTable.Columns
    For Each dt In SourceTables
    If i < dt.Rows.Count AndAlso dt.Columns.Contains(dcTarget.ColumnName) Then
    drTarget(dcTarget) = dt.Rows(i)(dcTarget.ColumnName)
    End If
    Next
    Next
    TargetTable.Rows.Add(drTarget)
    Next

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2013 3:00 AM

All replies

  • User1221318268 posted

    We can bind only one datasource at a time... so if u want to bind both then you need to use two controls (grids) to bind

    Tuesday, July 9, 2013 9:07 AM
  • User-1219482859 posted

    Hi,

    yes you can merge datatables,dataset and datarow array with different columns. Please check the below link:

    http://msdn.microsoft.com/en-us/library/aszytsd8(v=vs.80).aspx

    otherwise you can use below code to implement your column merge

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Create the dataset and put in the data. Normally you will just get this from a database query'

    Dim ds1 As New Data.DataSet
    Dim ds2 As New Data.DataSet

    Dim dt1 = ds1.Tables.Add()
    dt1.Columns.Add("ID", GetType(Int32))
    dt1.Columns.Add("ColA", GetType(String))
    dt1.Columns.Add("ColB", GetType(String))

    Dim dt2 = ds2.Tables.Add()
    dt2.Columns.Add("ColC", GetType(String))
    dt2.Columns.Add("ColD", GetType(String))

    dt1.Rows.Add(1, "a", "b")
    dt2.Rows.Add("c", "d")
    dt1.Rows.Add(2, "e", "f")
    dt2.Rows.Add("g", "h")

    'Sample data created, now to merge the results like you want'
    Dim dsNew As New Data.DataSet
    Dim dtNew = dsNew.Tables.Add(0)

    MergeColumns(dtNew, dt1, dt2)
    MergeData(dtNew, dt1, dt2)

    'Display the results'
    dsNew.AcceptChanges()
    Response.Write(dsNew.GetXml)

    End Sub


    Private Sub MergeColumns(ByVal TargetTable As Data.DataTable, ByVal ParamArray SourceTables() As Data.DataTable)
    For Each dtSource In SourceTables
    'Make a clone of the table, then steal the columns from the clone'
    Dim dtClone = dtSource.Clone

    While dtClone.Columns.Count > 0
    Dim dc = dtClone.Columns(0)

    dtClone.Columns.Remove(dc)

    TargetTable.Columns.Add(dc)
    End While
    Next
    End Sub


    Private Sub MergeData(ByVal TargetTable As Data.DataTable, ByVal ParamArray SourceTables() As Data.DataTable)
    'Determine the number of rows the final table will have'
    Dim nMaxRowCount = 0
    For Each dt In SourceTables
    If dt.Rows.Count > nMaxRowCount Then
    nMaxRowCount = dt.Rows.Count
    End If
    Next

    For i = 0 To nMaxRowCount - 1
    'Create a new row using column data from each table. Assumes the name is unique across tables.'
    Dim drTarget = TargetTable.NewRow
    For Each dcTarget As Data.DataColumn In TargetTable.Columns
    For Each dt In SourceTables
    If i < dt.Rows.Count AndAlso dt.Columns.Contains(dcTarget.ColumnName) Then
    drTarget(dcTarget) = dt.Rows(i)(dcTarget.ColumnName)
    End If
    Next
    Next
    TargetTable.Rows.Add(drTarget)
    Next

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2013 3:00 AM