Multitabled DataSet App : DataSet « ADO.Net « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » DataSet 
32.35.15.Multitabled DataSet App
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Configuration;


  public class MainForm : Form
  {
    private DataSet yourDBDS = new DataSet("YourDB");
    private SqlCommandBuilder sqlCBInventory;
    private SqlCommandBuilder sqlCBCustomers;
    private SqlCommandBuilder sqlCBOrders;

    private SqlDataAdapter invTableAdapter;
    private SqlDataAdapter custTableAdapter;
    private SqlDataAdapter ordersTableAdapter;

    private string cnStr = "SqlProvider";

    public MainForm()
    {
      InitializeComponent();
      
      invTableAdapter = new SqlDataAdapter("Select * from Inventory", cnStr);
      custTableAdapter = new SqlDataAdapter("Select * from Customers", cnStr);
      ordersTableAdapter = new SqlDataAdapter("Select * from Orders", cnStr);

      sqlCBInventory = new SqlCommandBuilder(invTableAdapter);
      sqlCBOrders = new SqlCommandBuilder(ordersTableAdapter);
      sqlCBCustomers = new SqlCommandBuilder(custTableAdapter);

      invTableAdapter.Fill(yourDBDS, "Inventory");
      custTableAdapter.Fill(yourDBDS, "Customers");
      ordersTableAdapter.Fill(yourDBDS, "Orders");

      BuildTableRelationship();

      dataGridViewInventory.DataSource = yourDBDS.Tables["Inventory"];
      dataGridViewCustomers.DataSource = yourDBDS.Tables["Customers"];
      dataGridViewOrders.DataSource = yourDBDS.Tables["Orders"];
    }
    private void BuildTableRelationship()
    {
      DataRelation dr = new DataRelation("CustomerOrder",yourDBDS.Tables["Customers"].Columns["CustID"],yourDBDS.Tables["Orders"].Columns["CustID"]);
      yourDBDS.Relations.Add(dr);

      dr = new DataRelation("InventoryOrder",yourDBDS.Tables["Inventory"].Columns["CarID"],yourDBDS.Tables["Orders"].Columns["CarID"]);
      yourDBDS.Relations.Add(dr);
    }
    private void btnUpdate_Click(object sender, EventArgs e)
    {
      try
      {
        invTableAdapter.Update(yourDBDS, "Inventory");
        custTableAdapter.Update(yourDBDS, "Customers");
        ordersTableAdapter.Update(yourDBDS, "Orders");
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }
    private void btnGetOrderInfo_Click(object sender, System.EventArgs e)
    {
      string strOrderInfo = string.Empty;
      DataRow[] drsCust = null;
      DataRow[] drsOrder = null;

      int custID = int.Parse(this.txtCustID.Text);

      drsCust = yourDBDS.Tables["Customers"].Select(string.Format("CustID = {0}", custID));
      strOrderInfo += string.Format("Customer {0}: {1} {2}\n"
        drsCust[0]["CustID"].ToString(),
        drsCust[0]["FirstName"].ToString().Trim(),
        drsCust[0]["LastName"].ToString().Trim());

      drsOrder = drsCust[0].GetChildRows(yourDBDS.Relations["CustomerOrder"]);

      foreach (DataRow r in drsOrder)
        strOrderInfo += string.Format("Order Number: {0}\n", r["OrderID"]);

      DataRow[] drsInv = drsOrder[0].GetParentRows(yourDBDS.Relations["InventoryOrder"]);
      foreach (DataRow r in drsInv)
      {
        strOrderInfo += r["Make"];
        strOrderInfo += r["Color"];
        strOrderInfo += r["PetName"];
      }
      MessageBox.Show(strOrderInfo, "Order Details");
    }
        private void InitializeComponent()
        {
          this.dataGridViewInventory = new System.Windows.Forms.DataGridView();
          this.label1 = new System.Windows.Forms.Label();
          this.label2 = new System.Windows.Forms.Label();
          this.dataGridViewCustomers = new System.Windows.Forms.DataGridView();
          this.label3 = new System.Windows.Forms.Label();
          this.dataGridViewOrders = new System.Windows.Forms.DataGridView();
          this.btnUpdateDatabase = new System.Windows.Forms.Button();
          this.btnGetOrderInfo = new System.Windows.Forms.Button();
          this.txtCustID = new System.Windows.Forms.TextBox();
          this.label4 = new System.Windows.Forms.Label();
          this.groupBox1 = new System.Windows.Forms.GroupBox();
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewInventory)).BeginInit();
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomers)).BeginInit();
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewOrders)).BeginInit();
          this.groupBox1.SuspendLayout();
          this.SuspendLayout();
          // 
          // dataGridViewInventory
          // 
          this.dataGridViewInventory.Location = new System.Drawing.Point(1234);
          this.dataGridViewInventory.Name = "dataGridViewInventory";
          this.dataGridViewInventory.Size = new System.Drawing.Size(50982);
          this.dataGridViewInventory.TabIndex = 0;
          this.dataGridViewInventory.Text = "dataGridView1";
          // 
          // label1
          // 
          this.label1.AutoSize = true;
          this.label1.Location = new System.Drawing.Point(1118);
          this.label1.Name = "label1";
          this.label1.Size = new System.Drawing.Size(8813);
          this.label1.TabIndex = 1;
          this.label1.Text = "Current Inventory";
          // 
          // label2
          // 
          this.label2.AutoSize = true;
          this.label2.Location = new System.Drawing.Point(12126);
          this.label2.Name = "label2";
          this.label2.Size = new System.Drawing.Size(9313);
          this.label2.TabIndex = 3;
          this.label2.Text = "Current Customers";
          // 
          // dataGridViewCustomers
          // 
          this.dataGridViewCustomers.Location = new System.Drawing.Point(13142);
          this.dataGridViewCustomers.Name = "dataGridViewCustomers";
          this.dataGridViewCustomers.Size = new System.Drawing.Size(50990);
          this.dataGridViewCustomers.TabIndex = 2;
          this.dataGridViewCustomers.Text = "dataGridView2";
          // 
          // label3
          // 
          this.label3.AutoSize = true;
          this.label3.Location = new System.Drawing.Point(12244);
          this.label3.Name = "label3";
          this.label3.Size = new System.Drawing.Size(7513);
          this.label3.TabIndex = 5;
          this.label3.Text = "Current Orders";
          // 
          // dataGridViewOrders
          // 
          this.dataGridViewOrders.Location = new System.Drawing.Point(13260);
          this.dataGridViewOrders.Name = "dataGridViewOrders";
          this.dataGridViewOrders.Size = new System.Drawing.Size(50990);
          this.dataGridViewOrders.TabIndex = 4;
          this.dataGridViewOrders.Text = "dataGridView3";
          // 
          // btnUpdateDatabase
          // 
          this.btnUpdateDatabase.Location = new System.Drawing.Point(407368);
          this.btnUpdateDatabase.Name = "btnUpdateDatabase";
          this.btnUpdateDatabase.Size = new System.Drawing.Size(11423);
          this.btnUpdateDatabase.TabIndex = 6;
          this.btnUpdateDatabase.Text = "Update Database";
          this.btnUpdateDatabase.Click += new System.EventHandler(this.btnUpdate_Click);
          // 
          // btnGetOrderInfo
          // 
          this.btnGetOrderInfo.Location = new System.Drawing.Point(8471);
          this.btnGetOrderInfo.Name = "btnGetOrderInfo";
          this.btnGetOrderInfo.Size = new System.Drawing.Size(11023);
          this.btnGetOrderInfo.TabIndex = 7;
          this.btnGetOrderInfo.Text = "Get Order Details";
          this.btnGetOrderInfo.Click += new System.EventHandler(this.btnGetOrderInfo_Click);
          // 
          // txtCustID
          // 
          this.txtCustID.Location = new System.Drawing.Point(8429);
          this.txtCustID.Name = "txtCustID";
          this.txtCustID.Size = new System.Drawing.Size(11020);
          this.txtCustID.TabIndex = 8;
          // 
          // label4
          // 
          this.label4.AutoSize = true;
          this.label4.Location = new System.Drawing.Point(1029);
          this.label4.Name = "label4";
          this.label4.Size = new System.Drawing.Size(6813);
          this.label4.TabIndex = 9;
          this.label4.Text = "Customer ID:";
          // 
          // groupBox1
          // 
          this.groupBox1.Controls.Add(this.btnGetOrderInfo);
          this.groupBox1.Controls.Add(this.label4);
          this.groupBox1.Controls.Add(this.txtCustID);
          this.groupBox1.Location = new System.Drawing.Point(15368);
          this.groupBox1.Name = "groupBox1";
          this.groupBox1.Size = new System.Drawing.Size(200100);
          this.groupBox1.TabIndex = 10;
          this.groupBox1.TabStop = false;
          this.groupBox1.Text = "Lookup Customer Order";
          // 
          // MainForm
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(534480);
          this.Controls.Add(this.groupBox1);
          this.Controls.Add(this.btnUpdateDatabase);
          this.Controls.Add(this.label3);
          this.Controls.Add(this.dataGridViewOrders);
          this.Controls.Add(this.label2);
          this.Controls.Add(this.dataGridViewCustomers);
          this.Controls.Add(this.label1);
          this.Controls.Add(this.dataGridViewInventory);
          this.Name = "MainForm";
          this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
          this.Text = "YourDB Database Manipulator";
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewInventory)).EndInit();
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewCustomers)).EndInit();
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewOrders)).EndInit();
          this.groupBox1.ResumeLayout(false);
          this.groupBox1.PerformLayout();
          this.ResumeLayout(false);
          this.PerformLayout();

        }

        private System.Windows.Forms.DataGridView dataGridViewInventory;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.DataGridView dataGridViewCustomers;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.DataGridView dataGridViewOrders;
        private System.Windows.Forms.Button btnUpdateDatabase;
        private System.Windows.Forms.Button btnGetOrderInfo;
        private System.Windows.Forms.TextBox txtCustID;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.GroupBox groupBox1;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
32.35.DataSet
32.35.1.Simple Query Dataset
32.35.2.Fill a DataSet
32.35.3.Find Rows In Data
32.35.4.Load XML to DataSet
32.35.5.DataSet Merge event
32.35.6.Merge two DataSet
32.35.7.Creating a Strongly Typed DataSet
32.35.8.DataSet Read with SqlDataAdapter
32.35.9.DataSet.DataTable count
32.35.10.DataSet.DataTable name
32.35.11.Mapping Table and Column Names Between a Data Source and DataSet
32.35.12.Retrieving Schema and Constraints for a DataSet
32.35.13.Read schema and reload data with DataSet
32.35.14.Navigating Between Parent and Child Tables in an Untyped DataSet
32.35.15.Multitabled DataSet App
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.