Multiple Data View : DataView « ADO.Net « C# / CSharp Tutorial






using System.Data.SqlClient;
using System.Data;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

  public class MultipleView : System.Windows.Forms.Form
  {
    private System.Windows.Forms.DataGrid gridArgentina;
    private System.Windows.Forms.DataGrid gridBrazil;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.DataGrid gridAll;

    public MultipleView()
    {
      this.gridArgentina = new System.Windows.Forms.DataGrid();
      this.gridBrazil = new System.Windows.Forms.DataGrid();
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.label3 = new System.Windows.Forms.Label();
      this.gridAll = new System.Windows.Forms.DataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.gridArgentina)).BeginInit();
      ((System.ComponentModel.ISupportInitialize)(this.gridBrazil)).BeginInit();
      ((System.ComponentModel.ISupportInitialize)(this.gridAll)).BeginInit();
      this.SuspendLayout();
      // 
      // gridArgentina
      // 
      this.gridArgentina.DataMember = "";
      this.gridArgentina.Location = new System.Drawing.Point(12, 32);
      this.gridArgentina.Size = new System.Drawing.Size(416, 88);
      this.gridArgentina.TabIndex = 0;
      // 
      // gridBrazil
      // 
      this.gridBrazil.DataMember = "";
      this.gridBrazil.Location = new System.Drawing.Point(12, 156);
      this.gridBrazil.Size = new System.Drawing.Size(416, 96);
      this.gridBrazil.TabIndex = 1;
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(12, 12);
      this.label1.Size = new System.Drawing.Size(156, 16);
      this.label1.Text = "Customers From Argentina:";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(12, 136);
      this.label2.Size = new System.Drawing.Size(156, 16);
      this.label2.Text = "Customers From Brazil:";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(12, 268);
      this.label3.Size = new System.Drawing.Size(156, 16);
      this.label3.Text = "All Customers:";
      // 
      // gridAll
      // 
      this.gridAll.DataMember = "";
      this.gridAll.Location = new System.Drawing.Point(12, 288);
      this.gridAll.Size = new System.Drawing.Size(416, 96);
      // 
      // MultipleView
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(440, 398);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.gridAll,
                                      this.label3,
                                      this.label2,
                                      this.label1,
                                      this.gridBrazil,
                                      this.gridArgentina});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.Text = "Multiple Views";
      this.Load += new System.EventHandler(this.MultipleView_Load);
      ((System.ComponentModel.ISupportInitialize)(this.gridArgentina)).EndInit();
      ((System.ComponentModel.ISupportInitialize)(this.gridBrazil)).EndInit();
      ((System.ComponentModel.ISupportInitialize)(this.gridAll)).EndInit();
      this.ResumeLayout(false);

    }

    private void MultipleView_Load(object sender, System.EventArgs e)
    {
      string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
      string SQL = "SELECT * FROM Customers";
      SqlConnection con = new SqlConnection(connectionString);
      SqlCommand com = new SqlCommand(SQL, con);
      SqlDataAdapter adapter = new SqlDataAdapter(com);
      DataSet ds = new DataSet("Northwind");      

      con.Open();
      adapter.Fill(ds, "Customers");
      con.Close();

      DataView viewArgentina = new DataView(ds.Tables["Customers"]);
      DataView viewBrazil = new DataView(ds.Tables["Customers"]);

      viewArgentina.RowFilter = "Country = 'Argentina'";
      viewBrazil.RowFilter = "Country = 'Brazil'";

      gridArgentina.DataSource = viewArgentina;
      gridBrazil.DataSource = viewBrazil;
      gridAll.DataSource = ds.Tables["Customers"].DefaultView;    
    }

    [STAThread]
    static void Main() 
    {
      Application.Run(new MultipleView());
    }
  }








32.41.DataView
32.41.1.Attach DataViews to two separate DataGrid controls
32.41.2.DataView Sorter and Filter
32.41.3.Create DataViews from DataTable
32.41.4.Loop through DataRowView in DataView
32.41.5.Multiple Data View