How to: Bind Editors to Data

  • 2 minutes to read

This example shows how to bind the TextEdit, SpinEdit and ComboBoxEdit to data obtained from an MS Access database.

NOTE

A complete sample project is available at https://github.com/DevExpress-Examples/how-to-bind-editors-to-data-e1723.

using System.Windows;

namespace DXEditors_DataBinding {
    public partial class Window1 : Window {
        DXEditors_DataBinding.nwindDataSet.ProductsDataTable dt;
        int currentRecord;
        public Window1() {
            InitializeComponent();
            currentRecord = 0;
            dt = new nwindDataSetTableAdapters.ProductsTableAdapter().GetData();
            DataContext = dt[currentRecord];
            comboBoxEdit1.ItemsSource = 
                new nwindDataSetTableAdapters.CategoriesTableAdapter().GetData();
        }

        private void prevButton_Click(object sender, RoutedEventArgs e) {
            currentRecord--;
            UpdateData();
            UpdateButtonState();
        }
        private void nextButton_Click(object sender, RoutedEventArgs e) {
            currentRecord++;
            UpdateData();
            UpdateButtonState();
        }
        private void UpdateData() {
            DataContext = dt[currentRecord];
        }
        private void UpdateButtonState() {
            prevButton.IsEnabled = currentRecord != 0;
            nextButton.IsEnabled = currentRecord != dt.Rows.Count - 1;
        }
    }
}