Scrolling Data Binding

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

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

    private void retrieve_Click(object sender, EventArgs e) {
        retrieve.Enabled = false;

        ds = CreateDataSet();

        textName.DataBindings.Add("Text", ds, "Products.ProductName");
        textQuan.DataBindings.Add("Text", ds, "Products.QuantityPerUnit");

        trackBar.Minimum = 0;
        trackBar.Maximum = this.BindingContext[ds, "Products"].Count - 1;

        textName.Enabled = true;
        textQuan.Enabled = true;
        trackBar.Enabled = true;
    }

    private DataSet CreateDataSet() {
        string customers = "SELECT * FROM Products";
        DataSet ds = new DataSet();

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
            SqlDataAdapter da = new SqlDataAdapter(customers, con);

            da.Fill(ds, "Products");
        }

        return ds;
    }

    private void trackBar_Scroll(object sender, EventArgs e) {
        this.BindingContext[ds, "Products"].Position = trackBar.Value;
    }

    private DataSet ds;
    private void InitializeComponent() {
        this.retrieve = new System.Windows.Forms.Button();
        this.textName = new System.Windows.Forms.TextBox();
        this.textQuan = new System.Windows.Forms.TextBox();
        this.trackBar = new System.Windows.Forms.TrackBar();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar)).BeginInit();
        this.SuspendLayout();

        this.retrieve.Location = new System.Drawing.Point(12, 12);
        this.retrieve.Size = new System.Drawing.Size(75, 23);
        this.retrieve.Text = "Retrieve";
        this.retrieve.UseVisualStyleBackColor = true;
        this.retrieve.Click += new System.EventHandler(this.retrieve_Click);

        this.textName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textName.Enabled = false;
        this.textName.Location = new System.Drawing.Point(12, 42);
        this.textName.Size = new System.Drawing.Size(268, 20);

        this.textQuan.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textQuan.Enabled = false;
        this.textQuan.Location = new System.Drawing.Point(13, 69);
        this.textQuan.Size = new System.Drawing.Size(267, 20);
        this.trackBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.trackBar.Enabled = false;
        this.trackBar.Location = new System.Drawing.Point(13, 96);
        this.trackBar.Size = new System.Drawing.Size(267, 45);
        this.trackBar.Scroll += new System.EventHandler(this.trackBar_Scroll);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 148);
        this.Controls.Add(this.trackBar);
        this.Controls.Add(this.textQuan);
        this.Controls.Add(this.textName);
        this.Controls.Add(this.retrieve);
        this.Name = "Form1";
        this.Text = "ScrollingDataBinding";
        ((System.ComponentModel.ISupportInitialize)(this.trackBar)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.Button retrieve;
    private System.Windows.Forms.TextBox textName;
    private System.Windows.Forms.TextBox textQuan;
    private System.Windows.Forms.TrackBar trackBar;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}