DataBinding Part 2(Navigation with Data Binding)

please refer to the first part of this sample

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

namespace DataBinding
{
    public partial class Form2 : Form
    {

        private DataTable dt = null;
        private BindingManagerBase storeBinding;

        public Form2()
        {
            InitializeComponent();

            this.dt = clsInstance.Products.getProducts();

            this.txtProductID.DataBindings.Add("Text" , dt, "ProductID");
            this.txtProductName.DataBindings.Add("Text", dt, "ProductName");
            this.txtProductPrice.DataBindings.Add("Text", dt, "ProductPrice");

            this.storeBinding = this.BindingContext[dt];
            
            this.storeBinding.PositionChanged += new EventHandler(storeBinding_PositionChanged);
        }

        private void storeBinding_PositionChanged(object sender, EventArgs e)
        {
            int intRow = 1 + storeBinding.Position;

            this.lblCurrentRow.Text = string.Format("{0} of {1}", intRow, storeBinding.Count);

        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (storeBinding.Position > 0)
                storeBinding.Position -= 1;
            else
                storeBinding.Position = storeBinding.Count-1; 
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (storeBinding.Position != storeBinding.Count - 1)
                storeBinding.Position += 1;
            else
                storeBinding.Position = 0;
        }
    }
}

0 comments:

Post a Comment