Tuesday, May 21, 2013

databinding

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.SqlClient;

namespace lab12_1
{
    public partial class Form1 : Form
    {
         SqlConnection con=new SqlConnection("Data Source=TANISH;Initial Catalog=department;User ID=sa;Password=seed");

         SqlDataAdapter da;
         DataSet ds = new DataSet();
       
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("insert into dept1 values("+textBox1.Text+",'"+textBox2.Text+"')", con);
            //da = new SqlDataAdapter("select * from dept1", con);
            //SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sqlda);
         //   DataSet ds = new DataSet();

            sqlda.Fill(ds,"dept1");
           
            dataGridView1.DataSource = ds.Tables[0];
           
           
        }

        private void button5_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from dept1", con);
            da.Fill(ds);
           // dataGridView1.Refresh();
            dataGridView1.DataSource = ds.Tables[0];
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Update dept1 set name='"+textBox2.Text+"' where id="+textBox1.Text+"", con);
            //da = new SqlDataAdapter("select * from dept1", con);
            //SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sqlda);
            //   DataSet ds = new DataSet();

            sqlda.Fill(ds, "dept1");

            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button4_Click(object sender, EventArgs e)
        {

            SqlDataAdapter sqlda = new SqlDataAdapter("delete from dept1 where id="+textBox1.Text+"" , con);
            //da = new SqlDataAdapter("select * from dept1", con);
            //SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sqlda);
            //   DataSet ds = new DataSet();

            sqlda.Fill(ds, "dept1");

            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox1.DataBindings.Add("text", ds.Tables[0], "id");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox2.DataBindings.Add("text", ds.Tables[0], "name");
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            BindingContext[ds.Tables[0]].Position = ds.Tables[0].Rows.Count - 1;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (BindingContext[ds.Tables[0]].Position < ds.Tables[0].Rows.Count - 1)
                BindingContext[ds.Tables[0]].Position = BindingContext[ds.Tables[0]].Position + 1;
        }

        private void button6_Click(object sender, EventArgs e)
        {
             if(BindingContext[ds.Tables[0]].Position>0)
                BindingContext[ds.Tables[0]].Position=BindingContext[ds.Tables[0]].Position-1;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            BindingContext[ds.Tables[0]].Position = 0;
        }
    }
}