(Domestic version)
Generally, we use inner join to connect data from different tables, and the resulting data is a row. If BIND is sent to the DataGrid Control, not that nice! Instead, if we use datarelation to replace this inner join and bind it to the DataGrid Control, it will have a better interface effect (see tables ).
The following describes how to use datarelation objectCode:
' Declare Variables
Dim CN As Sqlconnection
Dim Da1 As Sqldataadapter
Dim Da2 As Sqldataadapter
Dim DS As Dataset
Dim Parentcolumn As Datacolumn
Dim Childcolumn As Datacolumn
Dim Dr As System. Data. datarelation
Try
' Instantiate sqlconnection object
CN = New Sqlconnection ( " Data Source = localhost; initial catalog = northwind; Integrated Security = true; " )
CN. open ()
' Instantiate the data of an individual sqldataadapter In the MERs table
Da1 = New Sqldataadapter ( " Select * from MERs " , CN)
Da2 = New Sqldataadapter ( " Select * from orders " , CN)
' Instantiate DataSet object
DS = New Dataset
' Store the read data in dataset.
Da1.fill (DS, " MERs " )
Da2.fill (DS, " Orders " )
' Create linking column in datarelation
Parentcolumn = DS. Tables ( " MERs " ). Columns ( " Customerid " )
Childcolumn = DS. Tables ( " Orders " ). Columns ( " Customerid " )
' Instantiate datarelation object
Dr = New System. Data. datarelation ( " Detail-orders " , Parentcolumn, childcolumn)
DS. relations. Add (DR)
' Bind the data in dataset to the DataGrid Control.
Datagrid1.datasource = DS
Catch Ex As Sqlexception
'
MessageBox. Show (ex. Message, " Error " , Messageboxbuttons. OK, messageboxicon. Exclamation)
End Try