Attach DataViews to two separate DataGrid controls : DataView « ADO.Net « C# / CSharp Tutorial






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

public class MainClass : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.DataGrid dataGrid2;

  public MainClass()
  {
    InitializeComponent();
        string ConnectionString ="Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=localhost;";
        SqlConnection conn = new SqlConnection(ConnectionString);

        conn.Open();

        DataSet ds = new DataSet("Orders");

        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Orders", conn);

        adapter.Fill(ds, "Orders");

        DataView dataView1 = new DataView(ds.Tables["Orders"]);
        dataView1.Sort = "ShippedDate ASC";

        DataView dataView2 = new DataView(ds.Tables["Orders"]);
        dataView2.Sort = "EmployeeID ASC";

        dataGrid1.DataSource = dataView1;
        dataGrid2.DataSource = dataView2;    

  }

  private void InitializeComponent()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.dataGrid2 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(8, 8);
    this.dataGrid1.Size = new System.Drawing.Size(272, 248);
    // 
    // dataGrid2
    // 
    this.dataGrid2.DataMember = "";
    this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid2.Location = new System.Drawing.Point(288, 8);
    this.dataGrid2.Size = new System.Drawing.Size(312, 256);
    // 
    // MainClass
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(600, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.dataGrid2,
                                    this.dataGrid1});
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
    this.ResumeLayout(false);

  }

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

}








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