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;

namespace Stone
{
    public partial class Add_EditFactor : Form
    {
        DataRow Dr;
        public Add_EditFactor()
        {
            InitializeComponent();
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.StrIndex = "";
            this.StrFrm = "";
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtid.Text = "";
            txtkind.Text = "";
            txtcount.Text = "0";
            txtlength.Text = "0";
            txtwidth.Text = "0";
            txtmeasure.Text = "";
            txtprice.Text = "0";
            txtpriceall.Text = "0";
            
        }

        private void Add_EditFactor_Load(object sender, EventArgs e)
        {

            if (this.StrIndex == "Edit" && this.StrFrm == "SF")
            {
                try
                {
                    txtid.Text = Program.SF.GridViewDetail.CurrentRow.Cells[1].Value.ToString();
                    txtid.ReadOnly = true;
                    txtkind.Text = Program.SF.GridViewDetail.CurrentRow.Cells[2].Value.ToString();
                    txtcount.Text = Program.SF.GridViewDetail.CurrentRow.Cells[3].Value.ToString();
                    txtlength.Text = Program.SF.GridViewDetail.CurrentRow.Cells[4].Value.ToString();
                    txtwidth.Text = Program.SF.GridViewDetail.CurrentRow.Cells[5].Value.ToString();
                    txtmeasure.Text = Program.SF.GridViewDetail.CurrentRow.Cells[6].Value.ToString();
                    txtprice.Text = Program.SF.GridViewDetail.CurrentRow.Cells[7].Value.ToString();
                    txtpriceall.Text = Program.SF.GridViewDetail.CurrentRow.Cells[8].Value.ToString();
                    dateper.Text = Program.SF.GridViewDetail.CurrentRow.Cells[9].Value.ToString();
                    return;
                }
                catch (Exception ex) { }
            }




            if (this.StrIndex == "Edit" && this.StrFrm == "MGF")
            {
                try
                {
                    txtid.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[1].Value.ToString();
                    txtid.ReadOnly = true;
                    txtkind.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[2].Value.ToString();
                    txtcount.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[3].Value.ToString();
                    txtlength.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[4].Value.ToString();
                    txtwidth.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[5].Value.ToString();
                    txtmeasure.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[6].Value.ToString();
                    txtprice.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[7].Value.ToString();
                    txtpriceall.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[8].Value.ToString();
                    dateper.Text = Program.MGF.GridViewDetail.CurrentRow.Cells[9].Value.ToString();
                    return;
                }
                catch (Exception ex) { }
            }

            if (this.StrIndex == "Edit" && this.StrFrm == "")
            {
                txtid.Text = Program.F.GridViewFactor.CurrentRow.Cells[1].Value.ToString();
                txtid.ReadOnly = true;
                txtkind.Text = Program.F.GridViewFactor.CurrentRow.Cells[2].Value.ToString();
                txtcount.Text = Program.F.GridViewFactor.CurrentRow.Cells[3].Value.ToString();
                txtlength.Text = Program.F.GridViewFactor.CurrentRow.Cells[4].Value.ToString();
                txtwidth.Text = Program.F.GridViewFactor.CurrentRow.Cells[5].Value.ToString();
                txtmeasure.Text = Program.F.GridViewFactor.CurrentRow.Cells[6].Value.ToString();
                txtprice.Text = Program.F.GridViewFactor.CurrentRow.Cells[7].Value.ToString();
                txtpriceall.Text = Program.F.GridViewFactor.CurrentRow.Cells[8].Value.ToString();
                dateper.Text = Program.F.GridViewFactor.CurrentRow.Cells[9].Value.ToString();
                return;
            }


            dateper.Text = PDT.PersianDate.Miladi2Shamsi(DateTime.Now);
           
        }

        private void btnreg_Click(object sender, EventArgs e)
        {

            if (txtid.Text == "0" || txtcount.Text == "0" || txtprice.Text == "0" || dateper.Text == "")
            {
                MessageBox.Show("فیلدهای کد - تعداد - بهای واحد - تاریخ را بررسی کنید ، هیچ کدام نباید مقدار خالی داشته باشند", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            #region Operation Over SearchManagmentFactor

            Edit_SF();
            Add_SF();


            #endregion



            #region Operation Over ManagmentFactor

            Edit_MGF();
            Add_MGF();

            #endregion

            #region Operation Over Factor

            if (this.StrIndex == "Add" && this.StrFrm == "" )
            {
                DataRow[] DrArray = Program.FC.DSFactor.Tables["TDetailFactor"].Select("IdFactor = " + Program.F.TxtIDFactor.Text + " and " + "Id = " + txtid.Text);
                if (DrArray.Length != 0)
                {

                    DrArray[0]["Id"] = Int32.Parse(txtid.Text);
                    DrArray[0]["KindStone"] = txtkind.Text;
                    DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                    DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                    DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                    DrArray[0]["Measure"] = txtmeasure.Text;
                    DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                    DrArray[0]["PriceAll"] = txtpriceall.Text;
                    DrArray[0]["Date"] = dateper.Text;
                    Program.FC.DSFactor.Tables["TDetailFactor"].AcceptChanges();
                    Program.F.TxtSum.Text = "0";
                    for (int i = 0; i <= Program.F.GridViewFactor.RowCount - 1; i++)
                    {

                        Program.F.TxtSum.Text = (Int64.Parse(Program.F.TxtSum.Text) + Int64.Parse(Program.F.GridViewFactor.Rows[i].Cells[8].Value.ToString())).ToString();

                    }
                    MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;


                }

                Dr = Program.FC.DSFactor.Tables["TDetailFactor"].NewRow();
                Dr["IdFactor"] = Int64.Parse(Program.F.TxtIDFactor.Text);
                Dr["Id"] = Int32.Parse(txtid.Text);
                Dr["KindStone"] = txtkind.Text;
                Dr["Count"] = Int32.Parse(txtcount.Text);
                Dr["Length"] = Double.Parse(txtlength.Text);
                Dr["Width"] = Double.Parse(txtwidth.Text);
                Dr["Measure"] = txtmeasure.Text;
                Dr["Price"] = Int64.Parse(txtprice.Text);
                Dr["PriceAll"] = txtpriceall.Text;
                Dr["Date"] = dateper.Text;
                Program.FC.DSFactor.Tables["TDetailFactor"].Rows.Add(Dr);
                Program.F.TxtSum.Text = "0";
                for (int i = 0; i <= Program.F.GridViewFactor.RowCount - 1; i++)
                {

                    Program.F.TxtSum.Text = (Int64.Parse(Program.F.TxtSum.Text) + Int64.Parse(Program.F.GridViewFactor.Rows[i].Cells[8].Value.ToString())).ToString();

                }
            
                MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
               
                txtid.Text = "";
                txtkind.Text = "";
                txtcount.Text = "0";
                txtlength.Text = "0";
                txtwidth.Text = "0";
                txtmeasure.Text = "";
                txtprice.Text = "0";
                txtpriceall.Text = "0";
              
               
            }


            if (this.StrIndex == "Edit" && this.StrFrm == "")
            {
                DataRow[] DrArray = Program.FC.DSFactor.Tables["TDetailFactor"].Select("IdFactor = " + Program.F.TxtIDFactor.Text +" and "+ "Id = " + txtid.Text);
                DrArray[0]["Id"] = Int32.Parse(txtid.Text);
                DrArray[0]["KindStone"] = txtkind.Text;
                DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                DrArray[0]["Measure"] = txtmeasure.Text;
                DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                DrArray[0]["PriceAll"] = txtpriceall.Text;
                DrArray[0]["Date"] = dateper.Text;
                Program.FC.DSFactor.Tables["TDetailFactor"].AcceptChanges();
                Program.F.TxtSum.Text = "0";
                for (int i = 0; i <= Program.F.GridViewFactor.RowCount - 1; i++)
                {

                    Program.F.TxtSum.Text = (Int64.Parse(Program.F.TxtSum.Text) + Int64.Parse(Program.F.GridViewFactor.Rows[i].Cells[8].Value.ToString())).ToString();

                }
                MessageBox.Show("داده ها با موفقیت ثبت شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

            #endregion


        }

        private void txtprice_TextChanged(object sender, EventArgs e)
        {
            if (txtprice.Text == "") txtprice.Text = "0";

            if (txtcount.Text == "" || txtprice.Text == "") { txtpriceall.Text = "0"; return; }
            try
            {
                txtpriceall.Text = (double.Parse(txtprice.Text) * double.Parse(txtcount.Text) * double.Parse(txtmeasure.Text)).ToString();
            }
            catch (Exception ex)
            { }
        }

        private void txtcount_TextChanged(object sender, EventArgs e)
        {
            if (txtcount.Text == "") txtcount.Text = "0";


            if (txtcount.Text == "" || txtprice.Text == "") { txtpriceall.Text = "0"; return; }
       



            try
            {
                txtpriceall.Text = (double.Parse(txtprice.Text) * double.Parse(txtcount.Text) * double.Parse(txtmeasure.Text)).ToString();
            }
            catch (Exception ex)
            { }
        }

        private void txtkind_KeyPress(object sender, KeyPressEventArgs e)
        {
          
        }

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

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


        private int FindIndexRow(DataGridViewRow r)
        {
            for (int i = 0; i <= Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Count - 1; i++)
                if (r.Cells[0].Value.ToString() == Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[i][0].ToString())
                {

                    return i;
                }
            return -1;
        }

        private void Add_MGF()
        {


            if (this.StrIndex == "Add" && this.StrFrm == "MGF")
            {
                DataRow[] DrArray = Program.MGF.DS.Tables["TDetailFactor"].Select("IdFactor = " + Program.MGF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                if (DrArray.Length != 0)
                {
                    
                    DrArray[0]["Id"] = Int32.Parse(txtid.Text);
                    DrArray[0]["KindStone"] = txtkind.Text;
                    DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                    DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                    DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                    DrArray[0]["Measure"] = txtmeasure.Text;
                    DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                    DrArray[0]["PriceAll"] = txtpriceall.Text;
                    DrArray[0]["Date"] = dateper.Text;
                    Program.MGF.DS.Tables["TDetailFactor"].AcceptChanges();


                    DataRow[] DrArray1 = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + Program.MGF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                    DrArray1[0]["Id"] = Int32.Parse(txtid.Text);
                    DrArray1[0]["KindStone"] = txtkind.Text;
                    DrArray1[0]["Count"] = Int32.Parse(txtcount.Text);
                    DrArray1[0]["Length"] = Double.Parse(txtlength.Text);
                    DrArray1[0]["Width"] = Double.Parse(txtwidth.Text);
                    DrArray1[0]["Measure"] = txtmeasure.Text;
                    DrArray1[0]["Price"] = Int64.Parse(txtprice.Text);
                    DrArray1[0]["PriceAll"] = txtpriceall.Text;
                    DrArray1[0]["Date"] = dateper.Text;
                    Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                    Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);

                    long lng=0;
                    for (int i = 0; i <= Program.MGF.GridViewDetail.RowCount - 1; i++)
                    {

                        lng = lng + Int64.Parse(Program.MGF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                    }
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[Program.MGF.GridViewProperties.CurrentRow.Index][3] = lng.ToString();
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[Program.MGF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);
                    MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;


                }

                Dr = Program.MGF.DS.Tables["TDetailFactor"].NewRow();
                Dr["IdFactor"] = Int64.Parse(Program.MGF.GridViewProperties.CurrentRow.Cells[0].Value.ToString());
                Dr["Id"] = Int32.Parse(txtid.Text);
                Dr["KindStone"] = txtkind.Text;
                Dr["Count"] = Int32.Parse(txtcount.Text);
                Dr["Length"] = Double.Parse(txtlength.Text);
                Dr["Width"] = Double.Parse(txtwidth.Text);
                Dr["Measure"] = txtmeasure.Text;
                Dr["Price"] = Int64.Parse(txtprice.Text);
                Dr["PriceAll"] = txtpriceall.Text;
                Dr["Date"] = dateper.Text;
                Program.MGF.DS.Tables["TDetailFactor"].Rows.Add(Dr);

                DataRow Dr1;
                Dr1 = Program.FC.DSFactor.Tables["TDetailFactorManagment"].NewRow();
                Dr1["IdFactor"] = Int64.Parse(Program.MGF.GridViewProperties.CurrentRow.Cells[0].Value.ToString());
                Dr1["Id"] = Int32.Parse(txtid.Text);
                Dr1["KindStone"] = txtkind.Text;
                Dr1["Count"] = Int32.Parse(txtcount.Text);
                Dr1["Length"] = Double.Parse(txtlength.Text);
                Dr1["Width"] = Double.Parse(txtwidth.Text);
                Dr1["Measure"] = txtmeasure.Text;
                Dr1["Price"] = Int64.Parse(txtprice.Text);
                Dr1["PriceAll"] = txtpriceall.Text;
                Dr1["Date"] = dateper.Text;
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows.Add(Dr1);
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);




                long lng1 = 0;
                for (int i = 0; i <= Program.MGF.GridViewDetail.RowCount - 1; i++)
                {

                    lng1 = lng1 + Int64.Parse(Program.MGF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                }

                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[Program.MGF.GridViewProperties.CurrentRow.Index][3] = lng1.ToString();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[Program.MGF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng1.ToString()) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);



                MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtid.Text = "";
                txtkind.Text = "";
                txtcount.Text = "0";
                txtlength.Text = "0";
                txtwidth.Text = "0";
                txtmeasure.Text = "";
                txtprice.Text = "0";
                txtpriceall.Text = "0";


            }

            
        }

        private void Edit_MGF()
        {


            if (this.StrIndex == "Edit" && this.StrFrm == "MGF")
            {
                DataRow[] DrArray = Program.MGF.DS.Tables["TDetailFactor"].Select("IdFactor = " + Program.MGF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                DrArray[0]["Id"] = Int32.Parse(txtid.Text);
                DrArray[0]["KindStone"] = txtkind.Text;
                DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                DrArray[0]["Measure"] = txtmeasure.Text;
                DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                DrArray[0]["PriceAll"] = txtpriceall.Text;
                DrArray[0]["Date"] = dateper.Text;
                Program.MGF.DS.Tables["TDetailFactor"].AcceptChanges();


                DataRow[] DrArray1 = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + Program.MGF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                DrArray1[0]["Id"] = Int32.Parse(txtid.Text);
                DrArray1[0]["KindStone"] = txtkind.Text;
                DrArray1[0]["Count"] = Int32.Parse(txtcount.Text);
                DrArray1[0]["Length"] = Double.Parse(txtlength.Text);
                DrArray1[0]["Width"] = Double.Parse(txtwidth.Text);
                DrArray1[0]["Measure"] = txtmeasure.Text;
                DrArray1[0]["Price"] = Int64.Parse(txtprice.Text);
                DrArray1[0]["PriceAll"] = txtpriceall.Text;
                DrArray1[0]["Date"] = dateper.Text;
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);




                long lng = 0;
                for (int i = 0; i <= Program.MGF.GridViewDetail.RowCount - 1; i++)
                {

                    lng = lng + Int64.Parse(Program.MGF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                }
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[Program.MGF.GridViewProperties.CurrentRow.Index][3] = lng.ToString();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[Program.MGF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);
                MessageBox.Show("داده ها با موفقیت ثبت شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }



        }


        private void Add_SF()
        {


            if (this.StrIndex == "Add" && this.StrFrm == "SF")
            {
                DataRow[] DrArray = Program.SF.DSForOnlyOneFactor.Tables["TDetailFactor"].Select("IdFactor = " + Program.SF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                if (DrArray.Length != 0)
                {

                    DrArray[0]["Id"] = Int32.Parse(txtid.Text);
                    DrArray[0]["KindStone"] = txtkind.Text;
                    DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                    DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                    DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                    DrArray[0]["Measure"] = txtmeasure.Text;
                    DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                    DrArray[0]["PriceAll"] = txtpriceall.Text;
                    DrArray[0]["Date"] = dateper.Text;
                    Program.SF.DSForOnlyOneFactor.Tables["TDetailFactor"].AcceptChanges();


                    DataRow[] DrArray1 = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + Program.SF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                    DrArray1[0]["Id"] = Int32.Parse(txtid.Text);
                    DrArray1[0]["KindStone"] = txtkind.Text;
                    DrArray1[0]["Count"] = Int32.Parse(txtcount.Text);
                    DrArray1[0]["Length"] = Double.Parse(txtlength.Text);
                    DrArray1[0]["Width"] = Double.Parse(txtwidth.Text);
                    DrArray1[0]["Measure"] = txtmeasure.Text;
                    DrArray1[0]["Price"] = Int64.Parse(txtprice.Text);
                    DrArray1[0]["PriceAll"] = txtpriceall.Text;
                    DrArray1[0]["Date"] = dateper.Text;
                    Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                    Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);

                    long lng = 0;
                    for (int i = 0; i <= Program.SF.GridViewDetail.RowCount - 1; i++)
                    {

                        lng = lng + Int64.Parse(Program.SF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                    }
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][3] = lng.ToString();
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);

                    Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][3] = lng.ToString();
                    Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                    Program.SF.DSForPrint.Tables["TPropertiesFactor"].AcceptChanges();

                    MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;


                }

                Dr = Program.SF.DSForOnlyOneFactor.Tables["TDetailFactor"].NewRow();
                Dr["IdFactor"] = Int64.Parse(Program.SF.GridViewProperties.CurrentRow.Cells[0].Value.ToString());
                Dr["Id"] = Int32.Parse(txtid.Text);
                Dr["KindStone"] = txtkind.Text;
                Dr["Count"] = Int32.Parse(txtcount.Text);
                Dr["Length"] = Double.Parse(txtlength.Text);
                Dr["Width"] = Double.Parse(txtwidth.Text);
                Dr["Measure"] = txtmeasure.Text;
                Dr["Price"] = Int64.Parse(txtprice.Text);
                Dr["PriceAll"] = txtpriceall.Text;
                Dr["Date"] = dateper.Text;
                Program.SF.DSForOnlyOneFactor.Tables["TDetailFactor"].Rows.Add(Dr);

                DataRow Dr1;
                Dr1 = Program.FC.DSFactor.Tables["TDetailFactorManagment"].NewRow();
                Dr1["IdFactor"] = Int64.Parse(Program.SF.GridViewProperties.CurrentRow.Cells[0].Value.ToString());
                Dr1["Id"] = Int32.Parse(txtid.Text);
                Dr1["KindStone"] = txtkind.Text;
                Dr1["Count"] = Int32.Parse(txtcount.Text);
                Dr1["Length"] = Double.Parse(txtlength.Text);
                Dr1["Width"] = Double.Parse(txtwidth.Text);
                Dr1["Measure"] = txtmeasure.Text;
                Dr1["Price"] = Int64.Parse(txtprice.Text);
                Dr1["PriceAll"] = txtpriceall.Text;
                Dr1["Date"] = dateper.Text;
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows.Add(Dr1);
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);




                long lng1 = 0;
                for (int i = 0; i <= Program.SF.GridViewDetail.RowCount - 1; i++)
                {

                    lng1 = lng1 + Int64.Parse(Program.SF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                }

                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][3] = lng1.ToString();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][5] = PDT.PersianDate.c15d(lng1.ToString()) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);

                Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][3] = lng1.ToString();
                Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng1.ToString()) + " ریــال ";
                Program.SF.DSForPrint.Tables["TPropertiesFactor"].AcceptChanges();

                MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtid.Text = "";
                txtkind.Text = "";
                txtcount.Text = "0";
                txtlength.Text = "0";
                txtwidth.Text = "0";
                txtmeasure.Text = "";
                txtprice.Text = "0";
                txtpriceall.Text = "0";


            }


        }

        private void Edit_SF()
        {


            if (this.StrIndex == "Edit" && this.StrFrm == "SF")
            {
                DataRow[] DrArray = Program.SF.DSForOnlyOneFactor.Tables["TDetailFactor"].Select("IdFactor = " + Program.SF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                DrArray[0]["Id"] = Int32.Parse(txtid.Text);
                DrArray[0]["KindStone"] = txtkind.Text;
                DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                DrArray[0]["Measure"] = txtmeasure.Text;
                DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                DrArray[0]["PriceAll"] = txtpriceall.Text;
                DrArray[0]["Date"] = dateper.Text;
                Program.SF.DSForOnlyOneFactor.Tables["TDetailFactor"].AcceptChanges();


                DataRow[] DrArray1 = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + Program.SF.GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + txtid.Text);
                DrArray1[0]["Id"] = Int32.Parse(txtid.Text);
                DrArray1[0]["KindStone"] = txtkind.Text;
                DrArray1[0]["Count"] = Int32.Parse(txtcount.Text);
                DrArray1[0]["Length"] = Double.Parse(txtlength.Text);
                DrArray1[0]["Width"] = Double.Parse(txtwidth.Text);
                DrArray1[0]["Measure"] = txtmeasure.Text;
                DrArray1[0]["Price"] = Int64.Parse(txtprice.Text);
                DrArray1[0]["PriceAll"] = txtpriceall.Text;
                DrArray1[0]["Date"] = dateper.Text;
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);




                long lng = 0;
                for (int i = 0; i <= Program.SF.GridViewDetail.RowCount - 1; i++)
                {

                    lng = lng + Int64.Parse(Program.SF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                }
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][3] = lng.ToString();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);


                Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][3] = lng.ToString();
                Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                Program.SF.DSForPrint.Tables["TPropertiesFactor"].AcceptChanges();

                MessageBox.Show("داده ها با موفقیت ثبت شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }



        }

        private void txtid_TextChanged(object sender, EventArgs e)
        {
            if (txtid.Text == "") txtid.Text = "0";
        }

        private void txtlength_TextChanged(object sender, EventArgs e)
        {
            if (txtlength.Text == "") txtlength.Text = "0";
            try
            {
                txtmeasure.Text = (double.Parse(txtlength.Text) * double.Parse(txtwidth.Text)).ToString();
            }
            catch (Exception ex)
            { }
        }

        private void txtwidth_TextChanged(object sender, EventArgs e)
        {
            if (txtwidth.Text == "") txtwidth.Text = "0";
            try
            {
                txtmeasure.Text = (double.Parse(txtlength.Text) * double.Parse(txtwidth.Text)).ToString();
            }
            catch (Exception ex)
            { }
        }

        private void txtpriceall_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtmeasure_TextChanged(object sender, EventArgs e)
        {
            string strmeasure = txtmeasure.Text;


            string[] split = strmeasure.Split(new Char[] { '.' });

            if (split.Length == 1)
            {

                try
                {
                    txtpriceall.Text = (double.Parse(txtprice.Text) * double.Parse(txtcount.Text) * double.Parse(txtmeasure.Text)).ToString();
                }
                catch (Exception ex)
                { }

                return;
            }

            if (Int32.Parse(split[1]) < 100)
            {
                try
                {
                    txtpriceall.Text = (double.Parse(txtprice.Text) * double.Parse(txtcount.Text) * double.Parse(txtmeasure.Text)).ToString();
                }
                catch (Exception ex)
                { }
                return;
            }

            if (Int32.Parse(split[1]) < 1000)
            {
                split[1] = (Int32.Parse(split[1]) / 10).ToString();
                txtmeasure.Text = split[0] + "." + split[1];

                try
                {
                    txtpriceall.Text = (double.Parse(txtprice.Text) * double.Parse(txtcount.Text) * double.Parse(txtmeasure.Text)).ToString();
                }
                catch (Exception ex)
                { }
                return;
            }


            if (Int32.Parse(split[1]) < 10000)
            {
                split[1] = (Int32.Parse(split[1]) / 10).ToString();
                split[1] = (Int32.Parse(split[1]) / 10).ToString();
                txtmeasure.Text = split[0] + "." + split[1];

                try
                {
                    txtpriceall.Text = (double.Parse(txtprice.Text) * double.Parse(txtcount.Text) * double.Parse(txtmeasure.Text)).ToString();
                }
                catch (Exception ex)
                { }
                return;
            }


          


        }


    }



}
