using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace shop
{
    public partial class pardakhtghest : Form
    {
        public pardakhtghest()
        {
            InitializeComponent();
        }

        private void cls()
        {
            txtfamili.Text = txtnam.Text =
            txtmablagh.Text = mask1.Text = "";
        }

        void display()
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from pardakht", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "pardakht");
            dataGridView1.DataSource = ds.Tables["pardakht"].DefaultView;
            con1.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbCommand com = new OleDbCommand();
            com.CommandText = "insert into pardakht(tarikh,nam,famili,mablagh) values ('" + mask1.Text + "','" + txtnam.Text + "','" + txtfamili.Text + "','" + txtmablagh.Text + "')";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            display();
            cls();
        }

        private void pardakhtghest_Load(object sender, EventArgs e)
        {
            display();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbCommand com = new OleDbCommand();
            com.Connection = con;
            int item;
            com.CommandText = "select count(famili) from pardakht where famili='" + txtfamili.Text + "'";
            con.Open();
            item = (int)com.ExecuteScalar();
            con.Close();
            if (item != 0)
            {
                if (MessageBox.Show("آيا مي خواهيد شخص مورد نظز حذف شود؟", "توجه", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    com.CommandText = "delete from pardakht where tarikh='" + mask1.Text + "' and nam='" + txtnam.Text + "' and famili='" + txtfamili.Text +"'";
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                display();
                cls();
            }
            else
            {
                MessageBox.Show("!شخص مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            cls();
        }

        private void txtpish_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txtnam_TextChanged(object sender, EventArgs e)
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from pardakht where nam like'%" + txtnam.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "pardakht");
            dataGridView1.DataSource = ds.Tables["pardakht"].DefaultView;
            con1.Close();
        }

        private void txtfamili_TextChanged(object sender, EventArgs e)
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from pardakht where famili like'%" + txtfamili.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "pardakht");
            dataGridView1.DataSource = ds.Tables["pardakht"].DefaultView;
            con1.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from pardakht where nam='" + txtnam.Text + "' and famili='" + txtfamili.Text + "'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "pardakht");
            dataGridView1.DataSource = ds.Tables["pardakht"].DefaultView;
            con1.Close();

            decimal sum = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                sum += Convert.ToDecimal(dataGridView1.Rows[i].Cells["mablagh"].Value);
                dataGridView1.Rows[i].Cells["cod"].Value = i + 1;
            }
            lblkol.Text = sum.ToString("###,###,###,###");
        }
    }
}
