DataView Sorter and Filter : 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.OleDb;
using System.Data.SqlClient;

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

  public DataViewManagerDemo()
  {
    InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    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(448, 328);
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(480, 341);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.dataGrid1});
    this.Load += new System.EventHandler(this.DataViewManagerDemo_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);

  }

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

  private void DataViewManagerDemo_Load(object sender, System.EventArgs e)
    {
        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");

        DataViewManager dvm = new DataViewManager(ds);

        dvm.DataViewSettings["Orders"].RowFilter = "EmployeeID = 4";
        dvm.DataViewSettings["Orders"].Sort = "ShippedDate ASC";

        DataView dataView1 = dvm.CreateDataView(ds.Tables["Orders"]);

        dataGrid1.DataSource = dataView1;
    }
}










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