none
How to join two tables in a dataset RRS feed

  • Question

  •  Hi all,

    I have a datasete with two table from two different ms sql databases.

    how can i join these tables together and return it to a datagridview.

     

     

    Friday, January 23, 2009 8:44 AM

All replies

  • You'd have to create a new DataTable generated from the other 2 datatables.

    The DataSet doesn't support the "join" syntax, sorry.


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Saturday, January 24, 2009 2:27 AM
    Moderator
  • Try this

    myDataSet.Relations.Add(

    New DataRelation( "relDT1_DT2", DataTable1.Columns("ID"), DataTable2.Columns("ID"), True))

    once the relationship is established design Grid and the workaround is to use ComboBox Column Style to your "joined" Fields:
    Dim bs As BindingSource
    bs = New BindingSource
    With bs
        .DataSource = myDataSet
        .DataMember = DataTable1.TableName.ToString()
    End With

     

    Dim TxtBoxColumn As DataGridViewTextBoxColumn
    Dim cboColumn As DataGridViewComboBoxColumn

    TxtBoxColumn =

    New DataGridViewTextBoxColumn

     

    With TxtBoxColumn

    .DataPropertyName = DataTable1.Columns("ColumnName").ColumnName.ToString()

    .Name = DataTable1.Columns("ColumnName").ColumnName.ToString()

    .HeaderText =

    "Column Name"

    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

    myDataGridView.Columns.Add(TxtBoxColumn)

     

    'AutoSize Column

    .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

    .Visible =

    True

     

    End With

    cboColumn =

    New DataGridViewComboBoxColumn

     

    With cboColumn

    .DataSource = DataTable2

    .DisplayMember = DataTable2.Columns("DisplayColumn).ColumnName.ToString()

    .DataPropertyName = DataTable1.Columns("ID").ColumnName.ToString()

    .Name = DataTable1.Columns("ID").ColumnName.ToString()

    .HeaderText =

    "Header"

    .ValueMember = DataTable2.Columns("ID").ColumnName.ToString()

    myDataGridView.Columns.Add(cboColumn)

    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

    .Visible =

    True

     

    End With

     

     








    John
    • Proposed as answer by Codernater Tuesday, November 10, 2009 2:08 AM
    Thursday, October 8, 2009 6:11 PM
  • You could use LINQ to joind data
    Val Mazur (MVP) http://www.xporttools.net
    Friday, October 9, 2009 10:22 AM
    Moderator
  • This was a very old post, but that's ok.

    You could also try using the DataTable.Merge() method.
    ~~Bonnie Berent [C# MVP]

    (new blog --- not many posts yet --- be patient)
    geek-goddess-bonnie.blogspot.com
    Saturday, October 10, 2009 11:00 PM