Merge is one of the method of Dataset. The merge feature is basically used in applications where the concept of Master and Transaction table exists.
Pre-conditions for displaying two datasets in a single datagrid.
1) All the columns specified in the datagrid must be present in both datasets.
2) The data type of all columns in the datasets must be the same.
3) The column names should match.
A Merge walthrough.
Assume that we have two tables with the following structure:
Table1 Table2
Field1 int Field1 int
Field2 varchar(10) Field2 varchar(10)
Field3 varchar(20) Field3 varchar(20)
Using batch processing, we will have two tables–master and transaction. Both of these tables will have the same number of columns, same data types, and same field names.
Now, we need a datagrid to display records from the above tables. We assume that we have a datagrid that contains the definition for all columns.
We are mainly going to see the BindGrid method that binds the datasets with the datagrid. We will see how we can bind two datasets with a single datagrid.
The BindGrid method.
The BindGrid method.
Sub BindGrid()
Dim myConnection as New SqlConnection (strConn)
Dim DS1 As DataSet
Dim DS1 As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = new SqlDataAdapter(”exec s_get_table1″, MyConnection)
DS1 = new DataSet()
MyCommand.Fill(DS1, “MyTable”)
MyCommand = new SqlDataAdapter(”exec s_get_table2″, MyConnection)
DS2 = new DataSet()
MyCommand.Fill(DS2, “MyTable”)
‘Now this code works because the table name for both datasets are the same.
‘Also the data type and column name for both tables are the same.
ds1.merge(ds2)
MyDataGrid.DataSource=DS1.tables(0).DefaultView
MyDataGrid.DataBind()
End Sub
Posted by
Mahesh ( Tryangled )