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 TelBook
{
    public partial class fmgroup : Form
    {
        public fmgroup()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DATA.ACCDB;JET OLEDB:DATABASE PASSWORD=●●●●●●●●●●●●;");

        public void show()
        {
            try
            {
                
                string d;
                OleDbDataAdapter adap;
                DataSet ds = new DataSet();
                d = "select * from groups";
                adap = new OleDbDataAdapter(d, con);
                adap.Fill(ds, "groups");
                dgv.DataSource = ds.Tables["groups"];
                dgv.Columns["id"].HeaderText = "کد";
                dgv.Columns["groupname"].HeaderText = "نام گروه";
                dgv.Columns["id"].Width = 70;
                dgv.Columns["groupname"].Width = 210;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void fmgroup_Load(object sender, EventArgs e)
        {
            con.Open();
            show();
        }

        private void btadd_Click(object sender, EventArgs e)
        {
            try
            {
                string cmdstr;
                OleDbCommand cmdoledb = new OleDbCommand();
                cmdstr = "INSERT into groups(groupname) values('" + txgroup.Text + "')";
                cmdoledb.Connection = con;
                cmdoledb.CommandText = cmdstr;
                cmdoledb.ExecuteReader();
                show();
                MessageBox.Show("گروه جدید به ثبت رسید","System Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txgroup.ResetText(); txgroup.Focus();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txgroup_Enter(object sender, EventArgs e)
        {
            btadd.Enabled = true;
            btdel.Enabled = false;
        }

        private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            btadd.Enabled = false;
            btdel.Enabled = true;
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txgroup.ResetText();
            txgroup.Text = dgv.CurrentRow.Cells[1].Value.ToString();
            btedit.Enabled = true;

        }

        private void btedit_Click(object sender, EventArgs e)
        {
            try
            {
                {
                    string updateq = "update groups set groupname='" + txgroup.Text + "'where id=" + dgv.CurrentRow.Cells[0].Value.ToString() + "";
                    OleDbCommand uq = new OleDbCommand(updateq, con);
                    uq.ExecuteNonQuery();
                }
                {
                    string updateq = "update contact set fil1='" + txgroup.Text + "'where id=" + dgv.CurrentRow.Cells[0].Value.ToString() + "";
                    OleDbCommand uq = new OleDbCommand(updateq, con);
                    uq.ExecuteNonQuery();
                }
                show();
                btedit.Enabled = false;
                MessageBox.Show("عملیات ویرایش انجام شد", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btdel_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("آیا قصد دارید گروه انتخابی را حذف کنید ؟", "سوال بانک", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    OleDbCommand cs = new OleDbCommand("delete from groups where id=(" + dgv.CurrentRow.Cells[0].Value.ToString() + ")", con);
                    cs.ExecuteNonQuery();
                    show();
                    btdel.Enabled = false;
                    MessageBox.Show("عملیات حذف انجام شد", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
