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

namespace AjanseProject
{
    public partial class olaviat : Form
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public olaviat()
        {
            InitializeComponent();
        }

        private void olaviat_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=" + Environment.MachineName + ";Initial Catalog=ajans;Integrated Security=True";
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select rcode , rname, rfamily , olaviat from ranande";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "ranande");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ranande";
            dataGridView1.Columns["rcode"].HeaderText = "کد راننده";
            dataGridView1.Columns["rname"].HeaderText = "نام راننده";
            dataGridView1.Columns["rfamily"].HeaderText = "نام خانوادگی راننده";
            dataGridView1.Columns["olaviat"].HeaderText = "اولویت";

            textBox1.Select();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cmd.CommandText = "update ranande set olaviat = " + Convert.ToInt32(textBox1.Text) + " where rcode = " + Convert.ToInt32(textBox2.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("تغییر اولویت ها با موفقیت انجام شد", "نتیجه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ds.Clear();
            cmd.CommandText = "select rcode , rname, rfamily , olaviat from ranande";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "ranande");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ranande";
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           // cmd.CommandText = "update ranande set rname = '" + ds.Tables["ranande"].Rows[e.RowIndex][e.ColumnIndex] + "' where code = " + Convert.ToInt32(textBox2.Text);
           // cmd.ExecuteNonQuery();

           
            textBox2.Text = ds.Tables["ranande"].Rows[e.RowIndex]["rcode"].ToString();
            textBox1.Text = ds.Tables["ranande"].Rows[e.RowIndex]["olaviat"].ToString();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            bindingSource1.DataSource = ds;
            bindingSource1.DataMember = "ranande";
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["rcode"].HeaderText = "کد راننده";
            dataGridView1.Columns["rname"].HeaderText = "نام راننده";
            dataGridView1.Columns["rfamily"].HeaderText = "نام خانوادگی راننده";
            dataGridView1.Columns["olaviat"].HeaderText = "اولویت";

            bindingSource1.MoveNext();
             
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
        }
    }
}
