To fetch data from the server at run time by the click of a button, complete the following steps:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click ordersDataAdapter.Fill(dataSet1) Dim tb As DataTable = dataSet1.Tables("Orders") tb.PrimaryKey = New DataColumn() {tb.Columns("OrderID")} ordersDataGrid.DataSource = dataSet1 ordersDataGrid.DataMember = "Orders" Button1.Enabled = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void button1_Click(object sender, EventArgs e) { ordersDataAdapter.Fill(dataSet1); DataTable tb = dataSet1.Tables["Orders"]; tb.PrimaryKey = new DataColumn[] { tb.Columns["OrderID"] }; ordersDataGrid.DataSource = dataSet1; ordersDataGrid.DataMember = "Orders"; button1.Enabled = false; } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click ordDetDataAdapter.Fill(dataSet1) Dim tb As DataTable = dataSet1.Tables("Order Details") tb.PrimaryKey = New DataColumn() {tb.Columns("OrderID"), tb.Columns("ProductID")} ordDetDataGrid.DataSource = dataSet1.Tables("Order Details") Button2.Enabled = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void button2_Click(object sender, EventArgs e) { ordDetDataAdapter.Fill(dataSet1); DataTable tb = dataSet1.Tables["Order Details"]; tb.PrimaryKey = new DataColumn[] { tb.Columns["OrderID"], tb.Columns["ProductID"] }; ordDetDataGrid.DataSource = dataSet1.Tables["Order Details"]; button2.Enabled = false; } End Sub |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click Dim rel As New DataRelation("Orders - Order Details", dataSet1.Tables("Orders").Columns("OrderID"), dataSet1.Tables("Order Details").Columns("OrderID")) dataSet1.Relations.Add(rel) Button3.Enabled = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void button3_Click(object sender, EventArgs e) { DataRelation rel = new DataRelation("Orders - Order Details", dataSet1.Tables["Orders"].Columns["OrderID"], dataSet1.Tables["Order Details"].Columns["OrderID"]); dataSet1.Relations.Add(rel); button3.Enabled = false; } |
Notice that the view is inactive (no columns, no rows). This is because the underlying dataSet1 is empty.
This operation is performed by means of the OleDbDataAdapter object (ordersDataAdapter) which operates against Access C1NWind.mdb database. The view is still inactive, because the Order Details DataTable has yet to be added in dataSet1, which is referenced in the view's definition.
After this, action view is inactive because the join specified in the view's definition supposes presence of DataRelation between joined DataTable(s).