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 MyDataAccess;
using System.Data.SqlClient;

namespace Amozeshgah
{
    public partial class SearchingKarbar : Form
    {
        public SearchingKarbar()
        {
            InitializeComponent();
        }

        cDataAccess cData = new cDataAccess(clsPath.ConnectionString());
        static SqlConnection con = new SqlConnection(clsPath.ConnectionString());
        SqlDataAdapter da = new SqlDataAdapter("", con);
        DataSet ds = new DataSet();

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SearchingKarbar_Load(object sender, EventArgs e)
        {
            dgUser.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(50, 228, 125);
            dgUser.DefaultCellStyle.SelectionBackColor = Color.FromArgb(99, 145, 152);
            dgUser.DefaultCellStyle.WrapMode = DataGridViewTriState.False;
            dgUser.GridColor = Color.FromArgb(214, 214, 214);
            dgUser.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgUser.MultiSelect = false;
            dgUser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgUser.RowHeadersVisible = false;
            dgUser.ReadOnly = true;
            dgUser.AllowUserToResizeRows = false;
            DataSet ret = cData.ExecuteQuery("SELECT [ID],[Name],[Family],[CodeMeli],[TarikhTavalod],[Jensiyat],[Addres]FROM [tUsers]");
            dgUser.DataSource = ret.Tables[0];
        }

        private void dgUser_SelectionChanged(object sender, EventArgs e)
        {
            if (dgUser.SelectedRows.Count > 0)
            {

                
                txtName.Text = dgUser.CurrentRow.Cells["Name"].Value.ToString();
                txtFamily.Text = dgUser.CurrentRow.Cells["Family"].Value.ToString();
                txtTarikhTavalod.Text = dgUser.CurrentRow.Cells["TarikhTavalod"].Value.ToString();
                txtCodeMeli.Text = dgUser.CurrentRow.Cells["CodeMeli"].Value.ToString();
                txtJen.Text = dgUser.CurrentRow.Cells["Jensiyat"].Value.ToString();
                txtAddres.Text = dgUser.CurrentRow.Cells["Addres"].Value.ToString();
                
 

                try
                {
                    object ret = cData.ExecuteScalar("select Image from [tUsers] where ID =" + dgUser.SelectedRows[0].Cells[0].Value.ToString());
                    if (!(ret is DBNull))
                    {
                        picUser.Image = Image.FromStream(new System.IO.MemoryStream((byte[])ret));
                    }
                    else
                    {
                        picUser.Image = null;
                    }
                }
                catch (Exception)
                {

                    FarsiMessage.Show("لطفا رو سطر مورد نظر کلیک نمایید", "اخطار سیستم", FarsiMessageBoxButtons.OK, FarsiMessageBoxIcons.Error);
                }
            }
         }

        private void txtSearching_TextChanged(object sender, EventArgs e)
        {
            string s;
            ds.Clear();
            if (rdbCodeMeli.Checked)
            {
                s = "SELECT [ID],[Name],[Family],[CodeMeli],[TarikhTavalod],[Jensiyat],[Addres]FROM [tUsers] where CodeMeli like'%" + txtSearching.Text + "%'";
                da.SelectCommand.CommandText = s;

            }
            da.Fill(ds, "ID");
            dgUser.DataSource = ds.Tables["ID"];
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            SearchingKarbar_Load(sender,e);
            txtSearching.Text = "";

        }
      }
   }
