private void SetInitialRow_CalUnit()
    {
        DataTable dt = new DataTable();
        DataRow   dr;

        dt.Columns.Add("#", typeof(Int32));
        dt.Columns.Add("ItemID", typeof(Int32));
        dt.Columns.Add("From_Factor", typeof(Decimal));
        dt.Columns.Add("From_UnitID", typeof(Int32));
        dt.Columns.Add("From_UnitName", typeof(String));
        dt.Columns.Add("To_Factor", typeof(Decimal));
        dt.Columns.Add("To_UnitID", typeof(Int32));
        dt.Columns.Add("To_UnitName", typeof(String));
        dt.Columns.Add("Factor_Desc", typeof(String));


        dr = dt.NewRow();

        dr["#"]             = 0;
        dr["ItemID"]        = 0;
        dr["From_Factor"]   = 0;
        dr["From_UnitID"]   = 0;
        dr["From_UnitName"] = "";
        dr["To_Factor"]     = 0;
        dr["To_UnitID"]     = 0;
        dr["To_UnitName"]   = "";
        dr["Factor_Desc"]   = "";

        dt.Rows.Add(dr);

        ViewState["CulUnitTable"] = dt;
        GrdUnitCal.DataSource     = dt;
        GrdUnitCal.DataBind();
    }
    private void Fill_CalulateGrid()
    {
        try
        {
            DataTable dtCulUnitTable = (DataTable)ViewState["CulUnitTable"];
            DataRow   dtTableRow     = null;

            dtCulUnitTable.Rows.Clear();

            if (GridDetails.Rows.Count > 0)
            {
                for (int k = 0; k < GridDetails.Rows.Count; k++)
                {
                    //if (dtCulUnitTable.Rows.Count == 1 && string.IsNullOrEmpty(dtCulUnitTable.Rows[0]["From_UnitName"].ToString()))
                    //{
                    //    dtCulUnitTable.Rows.RemoveAt(0);
                    //}

                    //Save First Calculation Factor (Means : A=How Much B)
                    dtTableRow = dtCulUnitTable.NewRow();

                    dtTableRow["#"]             = 0;
                    dtTableRow["ItemID"]        = 0;
                    dtTableRow["From_Factor"]   = 1;
                    dtTableRow["From_UnitID"]   = Convert.ToInt32(GridDetails.Rows[k].Cells[11].Text);
                    dtTableRow["From_UnitName"] = Convert.ToString(GridDetails.Rows[k].Cells[12].Text);
                    dtTableRow["To_Factor"]     = Convert.ToDecimal((Convert.ToDecimal(GridDetails.Rows[k].Cells[13].Text) / Convert.ToDecimal(GridDetails.Rows[k].Cells[10].Text)).ToString("0.0000"));
                    dtTableRow["To_UnitID"]     = Convert.ToInt32(GridDetails.Rows[k].Cells[14].Text);
                    dtTableRow["To_UnitName"]   = Convert.ToString(GridDetails.Rows[k].Cells[15].Text);
                    if (string.IsNullOrEmpty(GridDetails.Rows[k].Cells[9].Text) == true || GridDetails.Rows[k].Cells[9].Text == "&nbsp;")
                    {
                        dtTableRow["Factor_Desc"] = "";
                    }
                    else
                    {
                        dtTableRow["Factor_Desc"] = Convert.ToString(GridDetails.Rows[k].Cells[9].Text);
                    }

                    dtCulUnitTable.Rows.Add(dtTableRow);

                    //Save First Calculation Factor (Means : B=How Much A)

                    dtTableRow = dtCulUnitTable.NewRow();

                    dtTableRow["#"]             = 0;
                    dtTableRow["ItemID"]        = 0;
                    dtTableRow["From_Factor"]   = 1;
                    dtTableRow["From_UnitID"]   = Convert.ToInt32(GridDetails.Rows[k].Cells[14].Text);
                    dtTableRow["From_UnitName"] = Convert.ToString(GridDetails.Rows[k].Cells[15].Text);
                    dtTableRow["To_Factor"]     = Convert.ToDecimal((Convert.ToDecimal(GridDetails.Rows[k].Cells[10].Text) / Convert.ToDecimal(GridDetails.Rows[k].Cells[13].Text)).ToString("0.0000"));
                    dtTableRow["To_UnitID"]     = Convert.ToInt32(GridDetails.Rows[k].Cells[11].Text);
                    dtTableRow["To_UnitName"]   = Convert.ToString(GridDetails.Rows[k].Cells[12].Text);
                    if (string.IsNullOrEmpty(GridDetails.Rows[k].Cells[9].Text) == true || GridDetails.Rows[k].Cells[9].Text == "&nbsp;")
                    {
                        dtTableRow["Factor_Desc"] = "";
                    }
                    else
                    {
                        dtTableRow["Factor_Desc"] = Convert.ToString(GridDetails.Rows[k].Cells[9].Text);
                    }

                    dtCulUnitTable.Rows.Add(dtTableRow);
                }

                //Add New Code For Unit Reverse Code
                decimal NewFactor1 = 0;
                decimal NewFactor2 = 0;
                if (GridDetails.Rows.Count > 1)
                {
                    for (int i = 0; i < GridDetails.Rows.Count; i++)
                    {
                        for (int j = i; j < GridDetails.Rows.Count; j++)
                        {
                            if (Convert.ToInt32(GridDetails.Rows[i].Cells[14].Text) != Convert.ToInt32(GridDetails.Rows[j].Cells[14].Text))
                            {
                                //Save First Calculation Factor (Means : A=How Much B)

                                NewFactor1 = Convert.ToDecimal(GridDetails.Rows[i].Cells[10].Text) / Convert.ToDecimal(GridDetails.Rows[i].Cells[13].Text);
                                NewFactor2 = Convert.ToDecimal(GridDetails.Rows[j].Cells[13].Text) / Convert.ToDecimal(GridDetails.Rows[j].Cells[10].Text);

                                dtTableRow = dtCulUnitTable.NewRow();

                                dtTableRow["#"]             = 0;
                                dtTableRow["ItemID"]        = 0;
                                dtTableRow["From_Factor"]   = 1;
                                dtTableRow["From_UnitID"]   = Convert.ToInt32(GridDetails.Rows[i].Cells[14].Text);
                                dtTableRow["From_UnitName"] = Convert.ToString(GridDetails.Rows[i].Cells[15].Text);
                                dtTableRow["To_Factor"]     = Convert.ToDecimal((NewFactor1 * NewFactor2).ToString("0.0000"));
                                dtTableRow["To_UnitID"]     = Convert.ToInt32(GridDetails.Rows[j].Cells[14].Text);
                                dtTableRow["To_UnitName"]   = Convert.ToString(GridDetails.Rows[j].Cells[15].Text);
                                dtTableRow["Factor_Desc"]   = "";

                                dtCulUnitTable.Rows.Add(dtTableRow);



                                //Save First Calculation Factor (Means : B=How Much A)

                                NewFactor1 = Convert.ToDecimal(GridDetails.Rows[i].Cells[13].Text) / Convert.ToDecimal(GridDetails.Rows[i].Cells[10].Text);
                                NewFactor2 = Convert.ToDecimal(GridDetails.Rows[j].Cells[10].Text) / Convert.ToDecimal(GridDetails.Rows[j].Cells[13].Text);

                                dtTableRow = dtCulUnitTable.NewRow();

                                dtTableRow["#"]             = 0;
                                dtTableRow["ItemID"]        = 0;
                                dtTableRow["From_Factor"]   = 1;
                                dtTableRow["From_UnitID"]   = Convert.ToInt32(GridDetails.Rows[j].Cells[14].Text);
                                dtTableRow["From_UnitName"] = Convert.ToString(GridDetails.Rows[j].Cells[15].Text);
                                dtTableRow["To_Factor"]     = Convert.ToDecimal((NewFactor1 * NewFactor2).ToString("0.0000"));
                                dtTableRow["To_UnitID"]     = Convert.ToInt32(GridDetails.Rows[i].Cells[14].Text);
                                dtTableRow["To_UnitName"]   = Convert.ToString(GridDetails.Rows[i].Cells[15].Text);
                                dtTableRow["Factor_Desc"]   = "";

                                dtCulUnitTable.Rows.Add(dtTableRow);
                            }
                        }
                    }
                }


                //ViewState["CulUnitTable"] = dtCurrentTable;
                GrdUnitCal.DataSource = null;
                GrdUnitCal.DataSource = dtCulUnitTable;
                GrdUnitCal.DataBind();
            }
            else
            {
                //ViewState["CulUnitTable"] = dtCurrentTable;
                GrdUnitCal.DataSource = dtCulUnitTable;
                GrdUnitCal.DataBind();
            }
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }