Set Data Binding with DataViewManager : DataGrid « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » DataGridScreenshots 
Set Data Binding with DataViewManager



using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Windows.Forms;

class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void getData_Click(object sender, EventArgs e) {
        string orders = "SELECT * FROM Orders";
        string customers = "SELECT * FROM Customers";

        using (SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["northwind"].ConnectionString)) {

            SqlDataAdapter da = new SqlDataAdapter(orders, con);

            DataSet ds = new DataSet();

            da.Fill(ds, "Orders");

            da = new SqlDataAdapter(customers, con);

            da.Fill(ds, "Customers");

            ds.Relations.Add("CustomerOrders",
                ds.Tables["Customers"].Columns["CustomerID"],
                ds.Tables["Orders"].Columns["CustomerID"]);

            DataViewManager dvm = new DataViewManager(ds);

            dvm.DataViewSettings["Customers"].RowFilter = "Country='UK'";

            dataGrid.SetDataBinding(dvm, "Customers");

        }

    }
    private void InitializeComponent() {
        this.getData = new System.Windows.Forms.Button();
        this.dataGrid = new System.Windows.Forms.DataGrid();
        this.SuspendLayout();
        // 
        // getData
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(684558);
        this.getData.Name = "getData";
        this.getData.TabIndex = 0;
        this.getData.Text = "Get Data";
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // dataGrid
        // 
        this.dataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGrid.Location = new System.Drawing.Point(1313);
        this.dataGrid.Name = "dataGrid";
        this.dataGrid.Size = new System.Drawing.Size(745534);
        this.dataGrid.TabIndex = 1;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(771593);
        this.Controls.Add(this.dataGrid);
        this.Controls.Add(this.getData);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }



    private System.Windows.Forms.Button getData;
    private System.Windows.Forms.DataGrid dataGrid;

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


           
       
Related examples in the same category
1.Fill a DataGrid
2.Set SelectCommand
3.Bind related tables
4.Set up relation between two tables
5.Set DataSource from DataSet
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.