Thursday, 18 October 2012

how to check all the chebox

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.SqlServerCe;
using System.Data;
using System.Configuration;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region
        SqlCeConnection   cn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["xx"].ToString());
        SqlCeDataAdapter da;
        SqlCeCommand cm;
        DataTable dt = new System.Data.DataTable();
        DataGridViewCheckBoxColumn dgh = new DataGridViewCheckBoxColumn();
        CheckBox cb = new CheckBox();
        int i;
        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            cm =new SqlCeCommand("insert into emp values(" +Convert.ToInt32( textBox1.Text) + ",'" + textBox2.Text + "')", cn);
            cm.ExecuteNonQuery();
            fill();
            MessageBox.Show("One record saved.", "Message", MessageBoxButtons.OK);
        }
       void fill()
        {
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            da = new SqlCeDataAdapter("select * from emp", cn);
            dt.Clear();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
       private void Form1_Load(object sender, EventArgs e)
       {
           fill();
           Rectangle rc = dataGridView1.GetCellDisplayRectangle(0, -1, true);
           cb.Size = new Size(13, 13);
           rc.X = 108;
           rc.Y = 4;
           cb.Location = rc.Location;
           dataGridView1.Controls.Add(cb);
           cb.CheckedChanged+=new EventHandler(cb_CheckedChanged);
       }
       private void cb_CheckedChanged(object sender, EventArgs e)
       {
           for (i = 0; i < dataGridView1.Rows.Count; i++)
               dataGridView1.Rows[i].Cells["Column1"].Value = cb.Checked;
           dataGridView1.EndEdit();
       }
       private void button2_Click(object sender, EventArgs e)
       {
           for (i = 0; i <dataGridView1.Rows.Count; i++)
           {
               if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true)
               {
                   cm = new SqlCeCommand("delete from emp where eid=" + Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value), cn);
                   cm.ExecuteNonQuery();
               }
           }
           MessageBox.Show("record(s) delted.");
           fill();
           clear1();
       }
       void clear1()
       {
           textBox1.Text = "";
           textBox2.Text = "";
           textBox1.Focus();
       }
       private void button3_Click(object sender, EventArgs e)
       {
           cm = new SqlCeCommand("update emp set name='" + textBox2.Text + "' where eid=" + textBox1.Text, cn);
           cm.ExecuteNonQuery();
           MessageBox.Show("One record updated.");
           textBox1.ReadOnly = false;
           clear1();
           fill();
       }

       private void dataGridView1_Click(object sender, EventArgs e)
       {
           textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
           textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
           textBox1.ReadOnly = true;
       }

     
    }
}

No comments:

Post a Comment