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;
using Stone.Class;
namespace Stone
{
    public partial class FrmSearchProducts : Form
    {


        Waiting wt = new Waiting();
        string XmlDataFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xml";
         int i = 0;
         string strlength = "";
        public string StrSql = "";

        public string StrCode = "";
        public string StrCount = "";
        public string StrWidth = "";
        public string StrHeight = "";
        public string StrPrice = "";
    
        public FrmSearchProducts()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            StrCode = "";
            StrCount = "";
            StrWidth = "";
            StrHeight = "";
            StrPrice = "";

            #region Collection Information For Search
            #region Code

            if (checkBoxCode.Checked == true)
            {
                if (radioButtonBigThan.Checked == true && txtCode1.TextLength > 0)
                {
                    StrCode = StrCode + "Id>" + txtCode1.Text; i++; 
                }

                if (radioButtonLowThan.Checked == true && txtCode1.TextLength > 0)
                {
                    StrCode = StrCode + "Id<" + txtCode1.Text; i++; 
                }

                if (radioButtonEqualThan.Checked == true && txtCode1.TextLength > 0)
                {
                    StrCode = StrCode + "Id=" + txtCode1.Text; i++; 
                }

                if (radioButtonBetween.Checked == true && txtCode1.TextLength > 0 && txtCode2.TextLength > 0)
                {
                    StrCode = StrCode + "Id>" + txtCode1.Text + " and Id<" + txtCode2.Text; i++; 
                }
                if (StrCode == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به کد را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            #endregion

            #region Kind
            if (checkBoxKind.Checked == true)
            {
                if (txtKind.Text == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به نوع سنگ را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    return;
                }
            }

            #endregion

            #region Count
            if (checkBoxCount.Checked == true)
            {
                if (radioButtonCountBigThan.Checked == true && txtCount1.TextLength > 0)
                {
                    StrCount = StrCount + "Count>" + txtCount1.Text; i++; 
                }

                if (radioButtonCountLowThan.Checked == true && txtCount1.TextLength > 0)
                {
                    StrCount = StrCount + "Count<" + txtCount1.Text; i++; 
                }

                if (radioButtonCountEqual.Checked == true && txtCount1.TextLength > 0)
                {
                    StrCount = StrCount + "Count=" + txtCount1.Text; i++; 
                }

                if (radioButtonCountBetween.Checked == true && txtCount1.TextLength > 0 && txtCount2.TextLength > 0)
                {
                    StrCount = StrCount + "Count>" + txtCount1.Text + " and Count<" + txtCount2.Text; i++; 
                }

                if ( StrCount == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به تعداد را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            #endregion

            #region Width

            // Width SQL

            if (checkBoxWidth.Checked == true)
            {
                if (radioButtonWidthBigThan.Checked == true && txtWidth1.TextLength > 0)
                {
                    StrWidth = StrWidth + "Width>" + txtWidth1.Text; i++; 
                }

                if (radioButtonWidthLowThan.Checked == true && txtWidth1.TextLength > 0)
                {
                   StrWidth = StrWidth + "Width<" + txtWidth1.Text; i++; 
                }

                if (radioButtonWidthEqual.Checked == true && txtWidth1.TextLength > 0)
                {
                     StrWidth = StrWidth + "Width=" + txtWidth1.Text; i++; 
                }

                if (radioButtonWidthBetween.Checked == true && txtWidth1.TextLength > 0 && txtWidth2.TextLength > 0)
                {
                    StrWidth = StrWidth + "Width>" + txtWidth1.Text + " and Width<" + txtWidth2.Text; i++; 

                }

                if (StrWidth == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به عرض را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
              
        
            }

#endregion 
          
            #region Height
            if (checkBoxHeight.Checked == true)
            {
                if (radioButtonHeightBigThan.Checked == true && txtHeight1.TextLength > 0)
                {
                     StrHeight = StrHeight + "Length>" + txtHeight1.Text; i++; 
                }

                if (radioButtonHeightLowThan.Checked == true && txtHeight1.TextLength > 0)
                {
                    StrHeight = StrHeight + "Length<" + txtHeight1.Text; i++; 
                }

                if (radioButtonHeightEqual.Checked == true && txtHeight1.TextLength > 0)
                {
                   StrHeight = StrHeight + "Length=" + txtHeight1.Text; i++; 
                }

                if (radioButtonHeightBetween.Checked == true && txtHeight1.TextLength > 0 && txtHeight2.TextLength > 0)
                {
                    StrHeight = StrHeight + "Length>" + txtHeight1.Text + " and Length<" + txtHeight2.Text; i++; 
                }

                if (StrHeight == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به طول را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            #endregion

            #region Measure
            if (checkBoxMeasure.Checked == true)
            {
                if (txtMeasure.Text == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به معیار را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            #endregion

            #region Price

            if (checkBoxPrice.Checked == true)
            {
                if (radioButtonPriceBigThan.Checked == true && txtPrice1.TextLength > 0)
                {
                    StrPrice = StrPrice + "Price>" + txtPrice1.Text; i++;
                }

                if (radioButtonPriceLowThan.Checked == true && txtPrice1.TextLength > 0)
                {
                     StrPrice = StrPrice + "Price<" + txtPrice1.Text; i++; 
                }

                if (radioButtonPriceEqual.Checked == true && txtPrice1.TextLength > 0)
                {
                     StrPrice = StrPrice + "Price=" + txtPrice1.Text; i++; 
                }

                if (radioButtonPriceBetween.Checked == true && txtPrice1.TextLength > 0 && txtPrice2.TextLength > 0)
                {
                    StrPrice = StrPrice + "Price>" + txtPrice1.Text + " and Price<" + txtPrice2.Text; i++; 
                }

                if (StrPrice == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به قیمت را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }
            #endregion

            #region Date

            if (checkBoxDate.Checked == true)
            {
                if (txtDate.Text == "")
                {
                   
                  MessageBox.Show("لطفاً موارد مربوط به معیار را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                  return;
                 
                }
            }

            #endregion

            #endregion
            Program.MP.DS.Tables["SearchProducts"].Clear();
            Program.MP.DSHelp.Tables["SearchProducts"].Clear();
            BGW.RunWorkerAsync();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxCode.Checked == true)
            {
                groupBoxCode.Enabled = true;
                
            }
            else { 
                
                groupBoxCode.Enabled = false;
                radioButtonBetween.Checked = false;
                radioButtonBigThan.Checked = false;
                radioButtonEqualThan.Checked = false;
                radioButtonLowThan.Checked = false;
                txtCode1.Text = "";
                txtCode2.Text = "";
             
             
            }
        }

        private void checkBoxKind_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxKind.Checked == true)
            {
                groupBoxKind.Enabled = true; 
            }
            else
            {
                groupBoxKind.Enabled = false;
                txtKind.Text = "";
           
            }
        }

        private void checkBoxCount_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxCount.Checked == true)
            {
                groupBoxCount.Enabled = true; 

            }
            else
            {

                groupBoxCount.Enabled = false;
                radioButtonCountBetween.Checked = false;
                radioButtonCountBigThan.Checked = false;
                radioButtonCountEqual.Checked = false;
                radioButtonCountLowThan.Checked = false;
                txtCount1.Text = "";
                txtCount2.Text = "";
            
            }
        }

        private void radioButtonBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonBetween.Checked == true)
            {
                txtCode2.Enabled = true;
            }
            else
            {
                txtCode2.Enabled = false;
                txtCode2.Text = "";
            }
        }

        private void radioButtonCountBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonCountBetween.Checked == true)
            {
                txtCount2.Enabled = true;
            }
            else
            {
                txtCount2.Enabled = false;
                txtCount2.Text = "";
            }
        }

        private void checkBoxWidthHeight_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxHeight.Checked == true)
            {
                groupBoxHeight.Enabled = true; 
            }
            else
            {
                groupBoxHeight.Enabled = false;

                radioButtonHeightBetween.Checked = false;
                radioButtonHeightBigThan.Checked = false;
                radioButtonHeightEqual.Checked = false;
                radioButtonHeightLowThan.Checked = false;
                txtHeight1.Text = "";
                txtHeight2.Text = "";
            }
        }

        private void radioButtonWidthBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonWidthBetween.Checked == true)
            {
                txtWidth2.Enabled = true;
            }
            else
            {
                txtWidth2.Enabled = false;
                txtWidth2.Text = "";
            }
        }

        private void checkBoxMeasure_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxMeasure.Checked == true)
            {
                groupBoxMeasure.Enabled = true; 
            }
            else
            {
                groupBoxMeasure.Enabled = false;
                txtMeasure.Text = "";
            
            }
        }

        private void checkBoxDate_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxDate.Checked == true)
            {
                groupBoxDate.Enabled = true; 
            }
            else
            {
                groupBoxDate.Enabled = false;
                txtDate.Text = "";
              
            }
        }

        private void radioButtonHeightBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonHeightBetween.Checked == true)
            {
                txtHeight2.Enabled = true;
            }
            else
            {
                txtHeight2.Enabled = false;
                txtHeight2.Text = "";
            }
        }

        private void radioButtonPriceBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonPriceBetween.Checked == true)
            {
                txtPrice2.Enabled = true;
            }
            else
            {
                txtPrice2.Enabled = false;
                txtPrice2.Text = "";
            }
        }

        private void checkBoxPrice_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxPrice.Checked == true)
            {
                groupBoxPrice.Enabled = true; 

            }
            else
            {

                groupBoxPrice.Enabled = false;
                radioButtonPriceBetween.Checked = false;
                radioButtonPriceBigThan.Checked = false;
                radioButtonPriceEqual.Checked = false;
                radioButtonPriceLowThan.Checked = false;
                txtPrice1.Text = "";
                txtPrice2.Text = "";
             
            }
        }

        private void groupBoxWidthHeight_Enter(object sender, EventArgs e)
        {

        }

        private void GridViewProduct_SelectionChanged(object sender, EventArgs e)
        {
            if (GridViewProduct.RowCount != 0)
            {

                BtnEdit.Enabled = true;
                BtnDelCurrent.Enabled = true;
                BtnAddToFactor.Enabled = true;
                PrePrint.Enabled = true;
                BtnDelAllProducts.Enabled = true;
            }
            else
            {

                BtnEdit.Enabled = false;
                BtnDelCurrent.Enabled = false;
                BtnAddToFactor.Enabled = false;
                PrePrint.Enabled = false;
                BtnDelAllProducts.Enabled = false;
            }
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            ADD_EditProducts EditWindow = new ADD_EditProducts(GridViewProduct.CurrentRow);
            EditWindow.StrIndex = "Edit";
            EditWindow.ShowDialog();
        }

        private void BtnDelCurrent_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا شما مطمئن هستید؟", "زمرد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                wt.CreateInForm(typeof(FrmSearchProducts), this, true, false, "DoWorkInFormCurrent");
                GridViewProduct.Refresh();
                Program.MP.GridViewProduct.Refresh();
            }
  
        }


        public void DoWorkInFormCurrent()
        {

            foreach (DataGridViewRow r in GridViewProduct.SelectedRows)
            {


                DataRow[] DrArray = Program.MP.DS.Tables["Products"].Select("Id = " + r.Cells[0].Value.ToString());

                int Length = DrArray.Length;
                while (Length != 0)
                {

                    int count = Program.MP.DS.Tables["Products"].Rows.Count - 1;

                    for (int i = 0; i <= count; i++)
                    {
                        if (r.Cells[0].Value.ToString() == Program.MP.DS.Tables["Products"].Rows[i][0].ToString())
                        {
                            Program.MP.DS.Tables["Products"].Rows[i].Delete();
                            Program.MP.DS.Tables["Products"].AcceptChanges();
                            break;
                        }
                    }


                    Length--;
                }
                GridViewProduct.Rows.Remove(r);
            }



            Program.MP.DS.Tables["Products"].AcceptChanges();
            Program.MP.DS.Tables["Products"].WriteXml(XmlDataFileName);




            Program.MP.DS.Tables["SearchProducts"].AcceptChanges();

        }

        private void BtnDelAllProducts_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("آیا شما مطمئن هستید؟", "زمرد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                wt.CreateInForm(typeof(FrmSearchProducts), this, true, false, "DoWorkInForm");
               
                Program.MP.GridViewProduct.Refresh();
                GridViewProduct.Refresh();
            }

        }


        public void DoWorkInForm()
        {

            GridViewProduct.SelectAll();

            foreach (DataGridViewRow r in GridViewProduct.SelectedRows)
            {


                DataRow[] DrArray = Program.MP.DS.Tables["Products"].Select("Id = " + r.Cells[0].Value.ToString());
                int Length = DrArray.Length;
                while (Length != 0)
                {

                    int count = Program.MP.DS.Tables["Products"].Rows.Count - 1;

                    for (int i = 0; i <= count; i++)
                    {
                        if (r.Cells[0].Value.ToString() == Program.MP.DS.Tables["Products"].Rows[i][0].ToString())
                        {
                            Program.MP.DS.Tables["Products"].Rows[i].Delete();
                            Program.MP.DS.Tables["Products"].AcceptChanges();
                            break;
                        }
                    }


                    Length--;
                }
                GridViewProduct.Rows.Remove(r);
            }



            Program.MP.DS.Tables["Products"].AcceptChanges();
            Program.MP.DS.Tables["Products"].WriteXml(XmlDataFileName);




            Program.MP.DS.Tables["SearchProducts"].AcceptChanges();
        }


        private void BtnFactorShow_Click(object sender, EventArgs e)
        {
            if (Program.F == null)
            {
                Program.F = new Factor();
                Program.F.Show();
                return;
            }

            if (Program.F.IsDisposed)
            {
                Program.F = new Factor();
                Program.F.Show();
                return;
            }

            Program.F.Show();
        }

        private void BtnAddToFactor_Click(object sender, EventArgs e)
        {
            DialogCount DC = new DialogCount(GridViewProduct.CurrentRow);
            DC.ShowDialog();
        }

        private void PrePrint_Click(object sender, EventArgs e)
        {
            DS.Tables.Clear();
            DataTable SearchProducts = Program.MP.DS.Tables["SearchProducts"].Copy();
            DS.Tables.Add(SearchProducts);
            stiReport1.Load(Application.StartupPath + "\\SearchProducts.mrt");
            stiReport1.RegData(DS);
            stiReport1.Show();
        }

        private void txtCode1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtCode2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtCount1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

        private void txtCount2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

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

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

        private void txtWidth1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtWidth2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtPrice1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

        private void txtPrice2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

 

        private void BGW_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
         
          
        }

        private void BGW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            GridViewProduct.DataSource = Program.MP.DS.Tables["SearchProducts"];
            MessageBox.Show("جستجو به اتمام رسید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
            GridViewProduct.Sort(GridViewProduct.Columns[0], ListSortDirection.Ascending);
        }

        private void BGW_DoWork(object sender, DoWorkEventArgs e)
        {


            #region Operation 
            int Index = 0;

            #region Code

            if (checkBoxCode.Checked)
            {
                DataRow[] Dr = Program.MP.DS.Tables["Products"].Select(StrCode);
                strlength = Dr.Length.ToString();

                for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                {
                    Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                }
                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }

            #endregion

            #region KindStone

            if (checkBoxKind.Checked)
            {
                if (Index == 0)
                {
                    for (int i = 0; i <= Program.MP.DS.Tables["Products"].Rows.Count - 1; i++)
                    {
                        if (OperateOnStringKindStone(Program.MP.DS.Tables["Products"].Rows[i][1].ToString(),txtKind.Text))
                        {

                            Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Program.MP.DS.Tables["Products"].Rows[i]);
                        
                        }
                    }
                }
                else
                {
                    for (int i = 0; i <= Program.MP.DS.Tables["SearchProducts"].Rows.Count - 1; i++)
                    {
                        if (OperateOnStringKindStone(Program.MP.DS.Tables["SearchProducts"].Rows[i][1].ToString(), txtKind.Text))
                        {

                            Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Program.MP.DS.Tables["SearchProducts"].Rows[i]);

                        }
                    }
                }

                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }


            #endregion

            #region Count

            if (checkBoxCount.Checked)
            {
                if (Index == 0)
                {
                    DataRow[] Dr = Program.MP.DS.Tables["Products"].Select(StrCount);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }

                else
                {

                    DataRow[] Dr = Program.MP.DS.Tables["SearchProducts"].Select(StrCount);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }


                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }


            #endregion

            #region Width

            if (checkBoxWidth.Checked)
            {
                if (Index == 0)
                {
                    DataRow[] Dr = Program.MP.DS.Tables["Products"].Select(StrWidth);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }

                else
                {

                    DataRow[] Dr = Program.MP.DS.Tables["SearchProducts"].Select(StrWidth);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }


                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }

            #endregion

            #region Height


            if (checkBoxHeight.Checked)
            {
                if (Index == 0)
                {
                    DataRow[] Dr = Program.MP.DS.Tables["Products"].Select(StrHeight);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }

                else
                {

                    DataRow[] Dr = Program.MP.DS.Tables["SearchProducts"].Select(StrHeight);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }


                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }


            #endregion

            #region Measure


            if (checkBoxMeasure.Checked)
            {
                if (Index == 0)
                {
                    for (int i = 0; i <= Program.MP.DS.Tables["Products"].Rows.Count - 1; i++)
                    {
                        if (OperateOnStringMeasure(Program.MP.DS.Tables["Products"].Rows[i][5].ToString(),txtMeasure.Text))
                        {

                            Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Program.MP.DS.Tables["Products"].Rows[i]);

                        }
                    }
                }
                else
                {
                    for (int i = 0; i <= Program.MP.DS.Tables["SearchProducts"].Rows.Count - 1; i++)
                    {
                        if (OperateOnStringMeasure(Program.MP.DS.Tables["SearchProducts"].Rows[i][5].ToString(),txtMeasure.Text))
                        {

                            Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Program.MP.DS.Tables["SearchProducts"].Rows[i]);

                        }
                    }
                }

                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }



            #endregion

            #region Price

            if (checkBoxPrice.Checked)
            {
                if (Index == 0)
                {
                    DataRow[] Dr = Program.MP.DS.Tables["Products"].Select(StrPrice);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }

                else
                {

                    DataRow[] Dr = Program.MP.DS.Tables["SearchProducts"].Select(StrPrice);
                    strlength = Dr.Length.ToString();

                    for (int Counter = 0; Counter <= Dr.Length - 1; Counter++)
                    {
                        Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Dr[Counter]);
                    }

                }


                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }


            #endregion

            #region Date


            if (checkBoxDate.Checked)
            {
                if (Index == 0)
                {
                    for (int i = 0; i <= Program.MP.DS.Tables["Products"].Rows.Count - 1; i++)
                    {
                        if (OperateOnStringDate(Program.MP.DS.Tables["Products"].Rows[i][7].ToString(),txtDate.Text))
                        {

                            Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Program.MP.DS.Tables["Products"].Rows[i]);

                        }
                    }
                }
                else
                {
                    for (int i = 0; i <= Program.MP.DS.Tables["SearchProducts"].Rows.Count - 1; i++)
                    {
                        if (OperateOnStringDate(Program.MP.DS.Tables["SearchProducts"].Rows[i][7].ToString(), txtDate.Text))
                        {

                            Program.MP.DSHelp.Tables["SearchProducts"].ImportRow(Program.MP.DS.Tables["SearchProducts"].Rows[i]);

                        }
                    }
                }

                Program.MP.DSHelp.Tables["SearchProducts"].AcceptChanges();
                Program.MP.DS.Tables.Remove("SearchProducts");
                Program.MP.DS.Tables.Add(Program.MP.DSHelp.Tables["SearchProducts"].Copy());
                Program.MP.DSHelp.Tables["SearchProducts"].Clear();
                Index++;
            }



            #endregion
            #endregion
        }

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

        private void radioButtonWidthBetween_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButtonWidthBetween.Checked == true)
            {
                txtWidth2.Enabled = true;
            }
            else
            {
                txtWidth2.Enabled = false;
                txtWidth2.Text = "";
            }
        }

        private void checkBoxWidth_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxWidth.Checked == true)
            {
                groupBoxWidth.Enabled = true;
            }
            else
            {
                groupBoxWidth.Enabled = false;
                radioButtonWidthBetween.Checked = false;
                radioButtonWidthBigThan.Checked = false;
                radioButtonWidthEqual.Checked = false;
                radioButtonWidthLowThan.Checked = false;
                txtWidth1.Text = "";
                txtWidth2.Text = "";
            }
        }

        private bool OperateOnStringKindStone(string Kind , string source)
        {
            String str = Kind;
            int i = str.IndexOf(source);
            if (i > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool OperateOnStringMeasure(string Measure, string source)
        {
            String str = Measure;
            int i = str.IndexOf(source);
            if (i > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool OperateOnStringDate(string Date, string source)
        {
            String str = Date;
            int i = str.IndexOf(source);
            if (i > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

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

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


        }

    }

