using com.hnshituo.core.webapp.vo;
using Common;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using JC_MeasuringSystem;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace StorageMeterSystem
{
    public partial class frmHotDeliveryActualQueryAll : Form
    {
        private MeterWorkStorageActualService actualService = new MeterWorkStorageActualService(); //预报服务
        private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
        private MeterBaseScalePointService scalePointService = new MeterBaseScalePointService(); //计量点信息服务
        private MeterWorkStorageActual storageActualG = new MeterWorkStorageActual(); //预报实体类

        private Log l = Log.GetInstance(); //日志
        private string userId = "", userName = ""; //用户ID用户名
        private CoreAppUser appUser = null;//登陆信息

        public frmHotDeliveryActualQueryAll()
        {
            InitializeComponent();
        }

        private void frmHotDeliveryActualQuery_Load(object sender, EventArgs e)
        {
            userId = ((ST_MainForm)(this.MdiParent)).UserID;
            userName = ((ST_MainForm)(this.MdiParent)).UserName;
            appUser = ((ST_MainForm)(this.MdiParent)).AppUser;

            dtStartTime.Value = Convert.ToDateTime((DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00"));
            dtEndTime.Value = Convert.ToDateTime((DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59"));

            this.ultraGridScale.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;
            this.ultraGridScale.DisplayLayout.GroupByBox.Hidden = true;
            #region 载入基础信息

            /*
            string sql = @"SELECT base_code baseCode,base_name baseName,memo FROM Com_Base_Info where p_base_code='001025' and valid_Flag='1'";
            PbModelDbService<List<ComBaseInfo>> pb = new PbModelDbService<List<ComBaseInfo>>();
            RESTfulResult<List<ComBaseInfo>> rmType = pb.executeSqlDataWf(sql);
            if (rmType.Succeed && rmType.Data != null && rmType.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbScaleState, rmType.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("产品信息载入失败");
                return;
            }

            */

            //计量点信息载入cmbPoint;cmbPointUp
            RESTfulResult<List<MeterBaseScalePoint>> rr = scalePointService.doQueryWf(new MeterBaseScalePoint { validFlag = "1", spotTypeNo = "001002004" });
            if (rr.Succeed && rr.Data != null && rr.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbPoint, rr.Data.ListToDataTable<MeterBaseScalePoint>(), "baseSpotNo", "baseSpotName");
                ClsControlPack.SetUltraComboDataSource(cmbPointUp, rr.Data.ListToDataTable<MeterBaseScalePoint>(), "baseSpotNo", "baseSpotName");
            }
            else
            {
                MessageBox.Show("计量点载入失败!");
                return;
            }

            //热送磅规格信息载入cmbSpcNameUp
            RESTfulResult<List<ComBaseInfo>> rrComSpcName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001018" });
            if (rrComSpcName.Succeed && rrComSpcName.Data != null && rrComSpcName.Data.Count > 0)
            {
                rrComSpcName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                ClsControlPack.SetUltraComboDataSource(cmbSpcNameUp, rrComSpcName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                ClsControlPack.SetUltraComboDataSource(cmbSpcName, rrComSpcName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅规格信息载入失败!");
                return;
            }

            //热送磅钢种信息载入cmbGradeNameUp
            RESTfulResult<List<ComBaseInfo>> rrComGradeName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001017" });
            if (rrComGradeName.Succeed && rrComGradeName.Data != null && rrComGradeName.Data.Count > 0)
            {
                rrComGradeName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                ClsControlPack.SetUltraComboDataSource(cmbGradeNameUp, rrComGradeName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                ClsControlPack.SetUltraComboDataSource(cmbGradeName, rrComGradeName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅钢种信息载入失败!");
                return;
            }

            //热送磅班次信息载入cmbMeterClassUp
            RESTfulResult<List<ComBaseInfo>> rrComMeterClass = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001043" });
            if (rrComMeterClass.Succeed && rrComMeterClass.Data != null && rrComMeterClass.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbMeterClassUp, rrComMeterClass.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                ClsControlPack.SetUltraComboDataSource(cmbMeterClass, rrComMeterClass.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅班次信息载入失败!");
                return;
            }

            //热送磅班组信息载入cmbMeterGroupUp
            RESTfulResult<List<ComBaseInfo>> rrComMeterGroop = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001044" });
            if (rrComMeterGroop.Succeed && rrComMeterGroop.Data != null && rrComMeterGroop.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbMeterGroupUp, rrComMeterGroop.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                ClsControlPack.SetUltraComboDataSource(cmbMeterGroup, rrComMeterGroop.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅班组信息载入失败!");
                return;
            }

            //数据状态
            ValueList vlistScale = new ValueList();
            vlistScale.ValueListItems.Add("", "全部");
            vlistScale.ValueListItems.Add("0", "无效");
            vlistScale.ValueListItems.Add("1", "有效");
            cmbScaleState.ValueList = vlistScale;
            cmbScaleState.SelectedIndex = 0;

            ValueList vlistFlag2 = new ValueList();
            vlistFlag2.ValueListItems.Add("", "全部");
            vlistFlag2.ValueListItems.Add("0", "未审核");
            vlistFlag2.ValueListItems.Add("1", "已审核");
            cmbCheckState.ValueList = vlistFlag2;
            cmbCheckState.SelectedIndex = 0;

            ValueList vlistFlag3 = new ValueList();
            vlistFlag3.ValueListItems.Add("0", "无效");
            vlistFlag3.ValueListItems.Add("1", "有效");
            this.ultraGridScale.DisplayLayout.Bands[0].Columns["valueFlag"].ValueList = vlistFlag3;

            ValueList vlistFlag4 = new ValueList();
            vlistFlag4.ValueListItems.Add("0", "未审核");
            vlistFlag4.ValueListItems.Add("1", "已审核");
            this.ultraGridScale.DisplayLayout.Bands[0].Columns["checkFlag"].ValueList = vlistFlag4;
            //ClsControlPack.SetUltraGridRowFilter(ref ultraGridScale, true);

            #endregion 载入基础信息
        }

        #region 点击事件

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "btnQuery": //btnInsert
                    {
                        btnQuery();
                        break;
                    }
                case "btnInsert": //新增
                    {
                        btnInsert();
                        break;
                    }
                case "btnCancel": //作废
                    {
                        btnUpdate("0");
                    }
                    break;

                case "btnUnCancel": //还原
                    {
                        btnUpdate("1");
                    }
                    break;

                case "btnUpdate": //修改
                    {
                        btnUpdate("");
                    }
                    break;

                case "btnCheck": //修改
                    {
                        btnUpdate("审核");
                    }
                    break;

                default: break;
            }
        }

        /// <summary>
        /// 行点击事件，给下面的区域赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraGridScale_ClickCell(object sender, ClickCellEventArgs e)
        {
            try
            {
                if (ultraGridScale.ActiveRow == null)
                {
                    return;
                }

                UltraGridRow ugr = ultraGridScale.ActiveRow;
                if (ugr.Cells["UNCK"].Value.ToString().ToUpper().Equals("TRUE"))
                {
                    ugr.Cells["UNCK"].Value = false;
                }
                else
                {
                    ugr.Cells["UNCK"].Value = true;
                }

                //界面赋值
                if (ugr != null)
                {
                    storageActualG.actualNo = ugr.Cells["actualNo"].Text.Trim(); //实绩编号
                    storageActualG.predictionNo = ugr.Cells["predictionNo"].Text.Trim(); //预报编号
                    storageActualG.specName = ugr.Cells["specName"].Text.Trim();         //规格
                    storageActualG.gradeName = ugr.Cells["gradeName"].Text.Trim();       //钢号
                    storageActualG.batchNo = ugr.Cells["batchNo"].Text.Trim();           //批次号
                    storageActualG.heatNo = ugr.Cells["heatNo"].Text.Trim();         //炉号
                    storageActualG.packageNo = ugr.Cells["packageNo"].Text.Trim();       //顺序号
                    storageActualG.blockNum = ugr.Cells["blockNum"].Text.Trim();         //总支数
                    storageActualG.theoreticalWeight = ugr.Cells["theoreticalWeight"].Text.Trim();         //理论重量
                    storageActualG.meterClass = ugr.Cells["meterClass"].Text.Trim();     //班次
                    storageActualG.meterGroup = ugr.Cells["meterGroup"].Text.Trim();     //班组
                    storageActualG.baseSpotNo = ugr.Cells["baseSpotNo"].Text.Trim();     //计量点的编号
                    storageActualG.baseSpotName = ugr.Cells["baseSpotName"].Text.Trim();     //计量点名称

                    //净重计算时；净重=采集重量+零点值-仪表底数
                    storageActualG.netWeight = Convert.ToDouble(ugr.Cells["netWeight"].Text.Trim()); //净重
                    storageActualG.grossWeight = Convert.ToDouble(ugr.Cells["grossWeight"].Text.Trim());  //采集的重量
                    storageActualG.referWeight = Convert.ToDouble(ugr.Cells["referWeight"].Text.Trim()); //仪表的底数
                    storageActualG.tareWeight = Convert.ToDouble(ugr.Cells["tareWeight"].Text.Trim()); //仪表零点值
                    storageActualG.actualWeight = storageActualG.netWeight;

                    if ("2".Equals(ugr.Cells["valueFlag"].Value + ""))
                    {
                        updateBoxUnEnable();
                    }
                    else
                    {
                        updateBoxEnable();
                    }
                    autoAutoFill(storageActualG); //点击行进行自动填充界面字段
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ultraGridScale_ClickCell方法处理异常：" + ex);
            }
        }

        /// <summary>
        /// 除顺序号，界面不可编辑
        /// </summary>
        private void updateBoxUnEnable()
        {
            cmbPointUp.Enabled = false;
            tbHeatNoUp.Enabled = false;
            cmbSpcNameUp.Enabled = false;
            cmbGradeNameUp.Enabled = false;
            tbTheoreticalWeightUp.Enabled = false;
            cmbMeterClassUp.Enabled = false;
            cmbMeterGroupUp.Enabled = false;
            //tbPackageNoUp.Enabled = false; //使用中的预报，顺序号可以修改，服务端取预报与实绩表中相对大的那一个值 +1  作为最新的顺序号
            tbBlockNumUp.Enabled = false;
        }

        /// <summary>
        /// 界面可编辑
        /// </summary>
        private void updateBoxEnable()
        {
            cmbPointUp.Enabled = true;
            tbHeatNoUp.Enabled = true;
            cmbSpcNameUp.Enabled = true;
            cmbGradeNameUp.Enabled = true;
            tbTheoreticalWeightUp.Enabled = true;
            cmbMeterClassUp.Enabled = true;
            cmbMeterGroupUp.Enabled = true;
            tbPackageNoUp.Enabled = true;
            tbBlockNumUp.Enabled = true;
        }

        /// <summary>
        /// 全选点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (UltraGridRow ugr in ultraGridScale.Rows)
            {
                if (!ugr.HiddenResolved)
                {
                    ugr.Cells["UNCK"].Value = cbAll.Checked;
                }
            }
        }

        /// <summary>
        /// 界面重量变化的时候，重新计算一遍重量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numCollectWeight_ValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(numCollectWeight.Text) || string.IsNullOrEmpty(numReferWeight.Text)
                || string.IsNullOrEmpty(numTareWeight.Text) || string.IsNullOrEmpty(numNetWeight.Text))
            {
                MessageBox.Show("采集重量、仪表底数、零点值、净重的值均不允许为空！");
                return;
            }
            //净重；净重=采集重量 + 零点值- 仪表底数
            numNetWeight.Value = (decimal)(Convert.ToDouble(numCollectWeight.Value) + Convert.ToDouble(numTareWeight.Value) - Convert.ToDouble(numReferWeight.Value));
        }
        
        /// <summary>
        /// 是否开启过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckFilter_CheckedChanged(object sender, EventArgs e)
        {
            if (ckFilter.Checked)
            {
                this.ultraGridScale.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True;
            }
            else
            {
                this.ultraGridScale.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.False;
            }

        }
        #endregion 点击事件

        /// <summary>
        /// 自动填充界面的数据
        /// </summary>
        private void autoAutoFill(MeterWorkStorageActual storageActualG)
        {
            if (storageActualG == null || string.IsNullOrEmpty(storageActualG.predictionNo) || string.IsNullOrEmpty(storageActualG.actualNo))
            {
                return;
            }
            cmbPointUp.Text = storageActualG.baseSpotName; //计量点名称1
            tbHeatNoUp.Text = storageActualG.heatNo; //炉号2
            cmbSpcNameUp.Text = storageActualG.specName; //规格名称3
            cmbGradeNameUp.Text = storageActualG.gradeName; //钢号名称4
            tbPackageNoUp.Text = storageActualG.packageNo;//顺序号5
            tbBlockNumUp.Text = storageActualG.blockNum;//总支数6
            cmbMeterClassUp.Text = storageActualG.meterClass;//班次7
            tbTheoreticalWeightUp.Text = storageActualG.theoreticalWeight + "";//理论重量8
            tbPredictionNo.Text = storageActualG.predictionNo; //预报编号9

            numCollectWeight.Value = (decimal)storageActualG.grossWeight;//采集的重量
            numTareWeight.Value = (decimal)storageActualG.tareWeight;//仪表零点值
            numReferWeight.Value = (decimal)storageActualG.referWeight;//仪表的底数
            numNetWeight.Value = (decimal)storageActualG.netWeight;//净重
            cmbMeterGroupUp.Text = storageActualG.meterGroup;//班组8
        }

        /// <summary>
        /// 界面的值给storageActual赋值；用于新增、修改
        /// </summary>
        /// <param name="storageActual"></param>
        private MeterWorkStorageActual setTableValue(MeterWorkStorageActual storageActual)
        {
            storageActual.predictionNo = tbPredictionNo.Text;
            storageActual.baseSpotName = cmbPointUp.Text;
            storageActual.baseSpotNo = cmbPointUp.Value + "";
            storageActual.heatNo = tbHeatNoUp.Text;
            storageActual.specName = cmbSpcNameUp.Text;
            storageActual.specNo = cmbSpcNameUp.Value + "";
            storageActual.gradeName = cmbGradeNameUp.Text;
            storageActual.gradeNo = cmbGradeNameUp.Value + "";
            storageActual.theoreticalWeight = tbTheoreticalWeightUp.Text;
            storageActual.meterClass = cmbMeterClassUp.Text;
            storageActual.meterGroup = cmbMeterGroupUp.Text;
            storageActual.packageNo = tbPackageNoUp.Text;
            storageActual.blockNum = tbBlockNumUp.Text;
            storageActual.grossWeight = Convert.ToDouble(numCollectWeight.Value); //采集的重量
            storageActual.referWeight = Convert.ToDouble(numReferWeight.Value);//仪表的底数
            storageActual.tareWeight = Convert.ToDouble(numTareWeight.Value);//仪表零点值
            //净重；净重=采集重量+零点值-仪表底数
            storageActual.netWeight = Convert.ToDouble(numNetWeight.Value);
            storageActual.actualWeight = storageActual.netWeight;
            return storageActual;
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="vFlag"></param>
        private void btnInsert()
        {
            if (string.IsNullOrEmpty(numCollectWeight.Text) || string.IsNullOrEmpty(numReferWeight.Text)
                || string.IsNullOrEmpty(numTareWeight.Text) || string.IsNullOrEmpty(numNetWeight.Text))
            {
                MessageBox.Show("采集重量、仪表底数、零点值、净重的值均不允许为空！");
                return;
            }
            MeterWorkStorageActual storageActual = new MeterWorkStorageActual();
            storageActual.createManName = userName;
            storageActual.createManNo = userId;

            storageActual = setTableValue(storageActual);
            RESTfulResult<string> rm = actualService.doAddHotDeliveryBelt(storageActual);

            if (rm.Succeed)
            {
                //actNo = rm.Data;
                MessageBox.Show("新增成功" + rm.Data.Split('#')[1]);
                return;
            }
            else
            {
                MessageBox.Show("保存失败：" + rm.Data);
                l.WriteLog(19, "保存失败：" + rm.Data + rm.Message);
                return;
            }
        }

        /// <summary>
        /// 修改数据(作废还原)
        /// </summary>
        /// <param name="vFlag"></param>
        private void btnUpdate(string vFlag)
        {
            try
            {
                int iCount = 0;
                List<MeterWorkStorageActual> lt = new List<MeterWorkStorageActual>();
                ultraGridScale.UpdateData();
                foreach (UltraGridRow ugr in ultraGridScale.Rows)
                {
                    if (ugr.Cells["UNCK"].Value.ToString().Trim().ToUpper() == "TRUE")
                    {
                        if (string.IsNullOrEmpty(vFlag) && "0".Equals(ugr.Cells["valueFlag"].Value + ""))
                        {
                            MessageBox.Show("已作废的数据，请先还原再进行修改！");
                            return;
                        }
                        if ("1".Equals(ugr.Cells["checkFlag"].Value + ""))
                        {
                            MessageBox.Show("已审核的数据不允许再修改，请联系计量系统管理员！");
                            return;
                        }
                        MeterWorkStorageActual preStorageUpdate = new MeterWorkStorageActual();
                        preStorageUpdate.actualNo = ugr.Cells["actualNo"].Text.Trim();
                        preStorageUpdate.predictionNo = ugr.Cells["predictionNo"].Text.Trim();
                        preStorageUpdate.updateManNo = userId;
                        preStorageUpdate.updateManName = userName;
                        if ("审核".Equals(vFlag))
                        {
                            preStorageUpdate.checkManNo = userId;
                            preStorageUpdate.checkManName = userName;
                            preStorageUpdate.checkFlag = vFlag;//已审核；服务端进行数据转换；将审核转换为 1
                        }
                        else if (!string.IsNullOrEmpty(vFlag))
                        {
                            preStorageUpdate.valueFlag = vFlag;
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(numCollectWeight.Text) || string.IsNullOrEmpty(numReferWeight.Text)
                               || string.IsNullOrEmpty(numTareWeight.Text) || string.IsNullOrEmpty(numNetWeight.Text))
                            {
                                MessageBox.Show("采集重量、仪表底数、零点值、净重的值均不允许为空！");
                                return;
                            }
                            preStorageUpdate = setTableValue(preStorageUpdate);
                        }

                        lt.Add(preStorageUpdate);
                        iCount++;
                    }
                }
                if (lt == null || lt.Count == 0)
                {
                    MessageBox.Show("请至少勾选一条数据后再进行修改或审核操作！");
                    return;
                }
                string msg = "审核".Equals(vFlag) ? "审核" : "修改";
                DialogResult dr = MessageBox.Show("共" + msg + "[" + iCount + "]条数据，是否继续操作！", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
                RESTfulResult<String> rms = actualService.doUpdateClient(lt);
                if (rms.Succeed)
                {
                    MessageBox.Show(msg + "成功！");
                    btnQuery();
                }
                else
                {
                    MessageBox.Show("操作失败：" + rms.Data);
                    l.WriteLog(19, "操作失败：" + rms.Data + rms.Message);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }



        /// <summary>
        /// 查询数据
        /// </summary>

        private void btnQuery()
        {
            DataTable dtScale = dataTable1.Clone();

            if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
            {
                MessageBox.Show("开始时间不能大于结束时间");
                return;
            }
            if ((dtEndTime.Value - dtStartTime.Value).Days > 30)
            {
                MessageBox.Show("时间跨度不能大于30天");
                return;
            }
            if (string.IsNullOrEmpty(cmbPoint.Value?.ToString()))
            {
                MessageBox.Show("请选择计量点");
                return;
            }

            CommonPage<MeterWorkStorageActual> cp = new CommonPage<MeterWorkStorageActual>();

            cp.param = new MeterWorkStorageActual(); //{ valueFlag = "1", baseSpotNo = cmbPoint.Value?.ToString() };
            cp.param.valueFlag = cmbScaleState.Value + "";
            cp.param.checkFlag = cmbCheckState.Value + "";
            cp.param.baseSpotNo = cmbPoint.Value + "";

            cp.param.meterClass = cmbMeterClass.Text + "";
            cp.param.meterGroup = cmbMeterGroup.Text + "";

            cp.param.specNo = cmbSpcName.Value + "";
            cp.param.gradeNo = cmbGradeName.Value + "";

            cp.param.startTime = dtStartTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cp.param.endTime = dtEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");

            RESTfulResult<List<MeterWorkStorageActual>> rms = actualService.doQuery(cp.param);
            if (rms.Succeed)
            {
                if (rms.Data != null && rms.Data.Count > 0)
                {
                    dtScale = rms.Data.ListToDataTable<MeterWorkStorageActual>();

                    ClsControlPack.CopyDataToDatatable(ref dtScale, ref dataTable1, true);
                    ClsControlPack.RefreshAndAutoSize(ultraGridScale);

                    foreach (UltraGridRow ugr in ultraGridScale.Rows)
                    {
                        //界面显示差值，单位为 kg
                        ugr.Cells["acDiffweight"].Value = double.Parse(ugr.Cells["theoreticalWeight"].Value.ToString()) - double.Parse(ugr.Cells["netWeight"].Value.ToString());
                        if (ugr.Cells["valueFlag"].Value.ToString() == "0")
                        {
                            ugr.Appearance.BackColor = Color.Red;
                        }
                        else if (ugr.Cells["checkFlag"].Value.ToString() == "1")
                        {
                            ugr.Appearance.BackColor = Color.YellowGreen;
                        }
                        else
                        {
                            ugr.Appearance.BackColor = Color.White;
                        }
                    }
                }
                else
                {
                    dtScale.Clear();
                    ClsControlPack.CopyDataToDatatable(ref dtScale, ref dataTable1, true);
                    ClsControlPack.RefreshAndAutoSize(ultraGridScale);
                }
            }
        }
    }
}