Exmple of BindingNavigator & DataGridView




//Drag DataGridView,TextBox & BindingNavigator
//Assign name as per this code
//====================================================================
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;
using System.Data.OleDb;
namespace DataGridiVewExample
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection();
        OleDbCommand com = new OleDbCommand();
        OleDbDataAdapter adp;
        DataTable dt = new DataTable();
        CurrencyManager cm;
        BindingSource bs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataGridDataSet.DataTab' table. You can move, or remove it, as needed.
            this.dataTabTableAdapter.Fill(this.dataGridDataSet.DataTab);
            try
            {
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataGrid.mdb";
                con.Open();
                com.Connection = con;
                com.CommandText = "Select * from DataTab";
                adp = new OleDbDataAdapter(com);
                adp.Fill(dt);
                bs.DataSource = dt;
                dataGridView1.DataSource = bs;
                textBox1.DataBindings.Add("Text", bs, "Name", true);
                cm = (CurrencyManager)this.BindingContext[bs];
                bindingNavigator1.BindingSource = bs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }
        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            cm.Position = 0;
        }
    }
}

No comments: