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 car
{
    public partial class chek : Form
    {
        public chek()
        {
            InitializeComponent();
        }

        OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=cardb.mdb");
        OleDbCommand cmd = new OleDbCommand();

        public void empty()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            txtdate.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
        }

        void display()
        {

            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter();
            adp.SelectCommand = new OleDbCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from chek";
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        private void chek_Load(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));

            display();

            dataGridViewX1.Columns[0].HeaderText = "کد چک";
            dataGridViewX1.Columns[1].HeaderText = "نام مشتری";
            dataGridViewX1.Columns[2].HeaderText = "تاریخ چک";
            dataGridViewX1.Columns[3].HeaderText = "مبلغ چک";
            dataGridViewX1.Columns[4].HeaderText = "نام خریدار چک";
            dataGridViewX1.Columns[5].HeaderText = "وجه نقدی";
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string query = "insert into chek (iD_chek,cname,tarekh,mablagh,name_kharedar,vajhe_nagde) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + txtdate.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
            OleDbCommand myCommand = new OleDbCommand(query, con);
            con.Open();
            myCommand.ExecuteNonQuery();
            con.Close();
            display();
            MessageBox.Show("داده‌ها با موفقیت ثبت گردید!");
            empty();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            string updatequery = "update chek set cname='" + textBox2.Text + "',tarekh='" + txtdate.Text + "',mablagh='" + textBox4.Text + "',name_kharedar='" + textBox5.Text + "',vajhe_nagde='" + textBox6.Text + "' where iD_chek='" + textBox1.Text + "'";
            OleDbCommand com = new OleDbCommand(updatequery, con);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            //*************************
            textBox1.Clear();
            textBox2.Clear();
            txtdate.Text="";
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            buttonX3.Enabled = false;
            display();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(dataGridViewX1.SelectedCells[0].Value);
            cmd.Parameters.Clear();
            cmd.Connection = con;
            cmd.CommandText = "delete from chek where iD_chek=@s";
            cmd.Parameters.AddWithValue("@s", x);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات حذف شد");
            display();
            empty();
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.dataGridViewX1.Width, this.dataGridViewX1.Height);
            dataGridViewX1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridViewX1.Width, this.dataGridViewX1.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            printDocument1.Print();
        }

        private void dataGridViewX1_MouseUp(object sender, MouseEventArgs e)
        {
            textBox1.Text = dataGridViewX1[0, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox2.Text = dataGridViewX1[1, dataGridViewX1.CurrentRow.Index].Value.ToString();
            txtdate.Text = dataGridViewX1[2, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox4.Text = dataGridViewX1[3, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox5.Text = dataGridViewX1[4, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox6.Text = dataGridViewX1[5, dataGridViewX1.CurrentRow.Index].Value.ToString();
        }

        private void textBox13_TextChanged(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter();
            adp.SelectCommand = new OleDbCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from chek where iD_chek like @N";
            adp.SelectCommand.Parameters.AddWithValue("@N", textBox13.Text + "%");
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }
    }
}
