Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding a Datagrid to a Data Relation

Status
Not open for further replies.

Gnism

Technical User
Jan 9, 2003
29
0
0
US
Hello all,
Can anyone tell me if it is possible to bind a datagrid to a data relation? If so, can you please give an example….Thanks – Nevin.
 
Code:
Dim myConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

Dim orderConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Temp\northwind.mdb;")

Try
            'Initialize DataSet.
            Dim custDS As New DataSet()

            'Initialize SqlDataAdapter with SQL and Connection String.
            Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", myConn)

            'Initialize OleDbDataAdapter with SQL and Connection string
            Dim orderDA As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Orders", orderConn)

            'Open connection.
            myConn.Open()
            orderConn.Open()

            'Fill DataSet with data from DataAdapter.
            custDA.Fill(custDS, "Customers")
            orderDA.Fill(custDS, "Orders")

            'Add relationship to the DataSet.
            Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
                                     custDS.Tables("Customers").Columns("CustomerID"), _
                                     custDS.Tables("Orders").Columns("CustomerID"))

            'Bind DataSet to the DataGrid.
            grdDetails.SetDataBinding(custDS, "Customers")

            Me.Size = New System.Drawing.Size(512, 458)

        Catch Excep As SqlClient.SqlException
            MessageBox.Show(Excep.Message & " " & Excep.Number, "SQL Server", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            'Close connection.
            myConn.Close()
            orderConn.Close()
End Try

Email: pankajmsm@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top