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

        SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=lavazem behdashti;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        void display()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from Tbl_Users";
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

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

            this.LayoutMdi(MdiLayout.Cascade);
            dataGridViewX1.Columns[0].HeaderText = "کد";
            dataGridViewX1.Columns[1].HeaderText = "نام کاربری";
            dataGridViewX1.Columns[2].HeaderText = "کلمه عبور";

            dataGridViewX1.Columns[0].Width = 50;
            dataGridViewX1.Columns[1].Width = 90;
            dataGridViewX1.Columns[2].Width = 90;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            cmd.Connection = con;
            cmd.Parameters.Clear();
            cmd.CommandText = "insert into Tbl_Users(id,UserName,Password)values (@q,@w,@e)";
            cmd.Parameters.AddWithValue("@q", textBox1.Text);
            cmd.Parameters.AddWithValue("@w", textBox2.Text);
            cmd.Parameters.AddWithValue("@e", textBox3.Text);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            display();
            MessageBox.Show("اطلاعات اضافه شد");

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            cmd.Parameters.Clear();
            cmd.Connection = con;
            cmd.CommandText = "update Tbl_Users set UserName='" + textBox2.Text + "',Password='" + textBox3.Text + "' where id=" + textBox1.Text;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات ویرایش شد");
            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 Tbl_Users where id=@s";
            cmd.Parameters.AddWithValue("@s", x);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات حذف شد");
            display();
        }

        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();
            textBox3.Text = dataGridViewX1[2, dataGridViewX1.CurrentRow.Index].Value.ToString();
        }
    }
}
