双gridview联动模块

主要是控制指针实现两个gridview联动

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Bizcent.Client.Common;
using Bizcent.Model.MDM;
using XtraMessageBox = Bizcent.Client.Common.XtraMessageBox;
using Bizcent.Model.Common;
using Bizcent.IManager.Common;
using Bizcent.IManager.MDM;
using System.Text.RegularExpressions;
using Bizcent.Common.OptReturn;
using System.Collections;
using Bizcent.IManager.SMS;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace Bizcent.Client.MDM
{
    public partial class FrmPriceChangeSKU : BaseForm
    {
        #region 窗体加载
        public FrmPriceChangeSKU()
        {
            InitializeComponent();
        }

        private void FrmPriceChangeSKU_Shown(object sender, EventArgs e)
        {
            try
            {
                if (!SetPurview())
                {
                    Close();
                    return;
                }
                LoadImportTemp();
                associateInitDeptCode();
                PromotionPeriodList = PeriodExhSetManager.LoadPromotionPeriodBill(dbNode);
                bsPromotionPeriod.DataSource = PromotionPeriodList;
                bsPromotionPeriod.ResetBindings(false);
            }
            catch (Exception ex) 
            {
                
            }
        }
        #endregion

        #region 接口和变量
        FormatMaster _PrintTemp = null;
        private IManagerIO IOManager { get; set; }
        NodeInfo dbNode = CurrentLogin.Node;
        private IManagerPeriodExhSet PeriodExhSetManager { get; set; }
        IList PromotionPeriodList = new List();
        private IManagerPriceChangeSKU PriceChangeSKUManager { get; set; }
        private IManagerUser UserManager { get; set; }
        private GridAssociate associateCategoryCode;
        private GridAssociate associateCategoryName;
        private IManagerAssociate AssociateManager { get; set; }
        #endregion

        #region 私有方法
        /// 
        /// 读取导入模板
        /// 
        private void LoadImportTemp()
        {
            IList types = IOManager.LoadIOFormatFrm(dbNode, this.Name);
            if (types != null && types.Count != 0)
            {
                FormatMaster fm = types[0] as FormatMaster;
                this.bsTencil.DataSource = types;
                _PrintTemp = fm;
                this.btnImportTencil.EditValue = fm.FormatCode;
            }
        }

        /// 
        /// 权限
        /// 
        /// 
        private bool SetPurview()
        {
            bool allow = true;
            IList purviewList = null;
            try
            {
                purviewList = UserManager.LoadUserModulePurviews(dbNode, CurrentLogin.CurrentUser.Employee.EmployeeCode, this.Tag.ToString());
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex);
                allow = false;
                return allow;
            }
            if (!purviewList.Contains(OperateInfo.Browse))
            {
                allow = false;
                return allow;
            }
            if (!purviewList.Contains(OperateInfo.Maintain))
            {
                allow = false;
                return allow;
            }
            return allow;
        }

        /// 
        /// 检查gvParent数据完整
        /// 
        private string CheckgvParent()
        {
            gvParent.CloseEditor();
            PriceChangeSKUInfo currentRow = bsParent.Current as PriceChangeSKUInfo;
            if (currentRow == null) { return null; }
            if (currentRow.CategoryCode == null || currentRow.CategoryCode == "")
            {
                return "二级品类编码不能为空!";

            }
            if (currentRow.CategoryName == null || currentRow.CategoryName == "")
            {
                return "二级品类名称不能为空!";
            }
            if (currentRow.BudgetLine == null || currentRow.BudgetLine.ToString() == "" || currentRow.BudgetLine == 0)
            {
                return "预算额度必须大于0!";
            }
            return "";
        }

        /// 
        /// 查询并绑定gvChild的数据
        /// 
        /// 
        private void LoadandBoundChildData(PriceChangeSKUInfo parentInfo)
        {
            if (parentInfo.PriceChangeCauseSKUList.Count > 0)
            {
                bsChild.DataSource = parentInfo.PriceChangeCauseSKUList;
            }
            else
            {
                IList childList = PriceChangeSKUManager.LoadChild(dbNode, parentInfo);
                bsChild.DataSource = childList;
            }
            bsChild.ResetBindings(false);
        }

        #endregion

        #region 按钮事件
        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gcParent.Focused)
            {
                FrmFind t = new FrmFind(gcParent);
                t.ShowDialog();
            }
            else
            {
                FrmFind f = new FrmFind(gcChild);
                f.ShowDialog();
            }
        }

        private void btnAddRow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (luePromotionPeriod.Text == null || luePromotionPeriod.Text == "")
                {
                    XtraMessageBox.ShowWarningMessage("请先选择档期!");
                    return;
                }
                string Msg = CheckgvParent();
                if (!string.IsNullOrEmpty(Msg))
                {
                    XtraMessageBox.Show(Msg);
                    return;
                }
                PriceChangeSKUInfo newRow = new PriceChangeSKUInfo();
                bsParent.Add(newRow);
                bsParent.ResetBindings(false);
                gvParent.FocusedColumn = colCategoryCode;
                gvParent.FocusedRowHandle = bsParent.Count - 1;
                gvParent.ShowEditor();

            }
            catch (Exception ex)
            {

                XtraMessageBox.Show(ex);
            }
        }

        private void btnDeleteRow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (bsParent.Count <= 0) { return; }
                if (XtraMessageBox.Show("确认删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    gvParent.CloseEditor();
                    PriceChangeSKUInfo currentRow = bsParent.Current as PriceChangeSKUInfo;
                    if (currentRow == null) { return; }
                    if (bsParent.Count > 0)
                    {
                        OperateReturnInfo opr = PriceChangeSKUManager.Delete(dbNode, currentRow);
                        if (opr.ReturnCode == OperateCodeEnum.Success)
                        {
                            bsParent.Remove(currentRow);
                            bsParent.ResetBindings(false);
                            if (bsParent.Count == 0)
                            { bsChild.Clear(); }
                            else
                            {
                                PriceChangeSKUInfo anotherCurrentRow = bsParent.Current as PriceChangeSKUInfo;
                                if (anotherCurrentRow.PriceChangeCauseSKUList.Count <= 0)
                                {
                                    IList childList = PriceChangeSKUManager.LoadChild(dbNode, anotherCurrentRow);
                                    anotherCurrentRow.PriceChangeCauseSKUList = childList;
                                    bsChild.DataSource = anotherCurrentRow.PriceChangeCauseSKUList;
                                }
                            }
                        }
                    }
                    bsParent.ResetBindings(false);
                    gvParent.ShowEditor();

                }
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show(ex);
            }
        }

        private void btnImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (_PrintTemp == null)
                {
                    XtraMessageBox.ShowWarningMessage("无导入模板!");
                    return;
                }

                FormatMaster fm = IOManager.LoadIOFormat(dbNode, this.btnImportTencil.EditValue.ToString());
                DataTable dt = new EasyImporter(IOManager).File2DataTable(fm.FormatCode);
                if (dt == null || dt.Rows.Count == 0) return;
                int ImportNumber = dt.Rows.Count;//导入的文件中数据的条数
                if (!dt.Columns.Contains("PromotionPeriodCode") || !dt.Columns.Contains("SubID") || !dt.Columns.Contains("CategoryCode") || !dt.Columns.Contains("BudgetLine")
                     || !dt.Columns.Contains("ChangeCauseCode") || !dt.Columns.Contains("PlanSKUNum") || !dt.Columns.Contains("SKULowerLimit") || !dt.Columns.Contains("SKUUpperLimit")
                    || !dt.Columns.Contains("PromotionThemeCode"))
                {
                    XtraMessageBox.ShowErrorMessage("导入必须包含字段:促销主题编码,档期编码,子档号,二级品类编码,预算额度,变价原因编码,规划SKU数量,选品SKU下限,选品SKU上限!");
                    return;
                }
                List errorInfos = new List();
                IList resultList = new List();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    if (dr == null) continue;

                    #region 验证数据有效性

                    if (!string.IsNullOrEmpty(dr["PromotionThemeCode"].ToString()))
                    {
                        string PromotionThemeCode = PriceChangeSKUManager.LoadPromotionThemeCode(dbNode, dr["PromotionThemeCode"].ToString());
                        if (string.IsNullOrEmpty(PromotionThemeCode))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "促销主题编码不存在!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "促销主题编码不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["PromotionPeriodCode"].ToString()))
                    {
                        bool IsExist = false;
                        foreach (PromotionPeriodInfo item in PromotionPeriodList)
                        {
                            if (item.PromotionPeriodCode == dr["PromotionPeriodCode"].ToString())
                            {
                                IsExist = true;
                            }
                        }
                        if (!IsExist)
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "档期编码不存在!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "档期编码不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["SubID"].ToString()))
                    {
                        bool IsExist = false;
                        foreach (PromotionPeriodInfo item in PromotionPeriodList)
                        {
                            if (item.SubID.ToString() == dr["SubID"].ToString())
                            {
                                IsExist = true;
                            }
                        }
                        if (!IsExist)
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "子档号不存在!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "子档号不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["CategoryCode"].ToString()))
                    {
                        string CategoryCode = PriceChangeSKUManager.LoadtbGoodsCategory(dbNode, dr["CategoryCode"].ToString());
                        if (string.IsNullOrEmpty(CategoryCode))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "二级分类编码不存在!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "二级分类编码不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["BudgetLine"].ToString()))
                    {
                        string BudgetLine = dr["BudgetLine"].ToString();
                        string pattern = @"^[0-9]*[1-9][0-9]*$";
                        if (!Regex.IsMatch(BudgetLine, pattern))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "预算额度只能输入正整数!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "预算额度不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["ChangeCauseCode"].ToString()))
                    {
                        string ChangeCauseCode = PriceChangeSKUManager.LoadChangePriceCause(dbNode, dr["ChangeCauseCode"].ToString());
                        if (string.IsNullOrEmpty(ChangeCauseCode))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "变价原因编码不存在!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "变价原因编码不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["PlanSKUNum"].ToString()))
                    {
                        string PlanSKUNum = dr["PlanSKUNum"].ToString();
                        string pattern = "^[1-9]\\d*$|0$";
                        if (!Regex.IsMatch(PlanSKUNum, pattern))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "规划SKU数量只能输入非负整数!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "规划SKU数量不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["SKULowerLimit"].ToString()))
                    {
                        string SKULowerLimit = dr["SKULowerLimit"].ToString();
                        string pattern = "^[1-9]\\d*$|0$";
                        if (!Regex.IsMatch(SKULowerLimit, pattern))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "选品SKU下限只能输入非负整数!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "选品SKU下限不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["SKUUpperLimit"].ToString()))
                    {
                        string SKUUpperLimit = dr["SKUUpperLimit"].ToString();
                        string pattern = "^[1-9]\\d*$|0$";
                        if (!Regex.IsMatch(SKUUpperLimit, pattern))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "选品SKU上限只能输入非负整数!");
                            continue;
                        }
                    }
                    else
                    {
                        errorInfos.Add("第" + (i + 1) + "行" + "选品SKU上限不能为空!");
                        continue;
                    }
                    if (!string.IsNullOrEmpty(dr["SKUUpperLimit"].ToString()) && !string.IsNullOrEmpty(dr["SKULowerLimit"].ToString()))
                    {
                        if (int.Parse(dr["SKUUpperLimit"].ToString()) < int.Parse(dr["SKULowerLimit"].ToString()))
                        {
                            errorInfos.Add("第" + (i + 1) + "行" + "选品SKU上限必须大于商品SKU下限!");
                            continue;
                        }
                    }
                    #endregion

                    PriceChangeCauseSKUInfo info = new PriceChangeCauseSKUInfo();
                    info.ChangeCauseCode = dr["ChangeCauseCode"].ToString();
                    info.SKULowerLimit = int.Parse(dr["SKULowerLimit"].ToString());
                    info.SKUUpperLimit = int.Parse(dr["SKUUpperLimit"].ToString());
                    info.PlanSKUNum = int.Parse(dr["PlanSKUNum"].ToString());

                    PriceChangeSKUInfo mainInfo = new PriceChangeSKUInfo();
                    mainInfo.PromotionThemeCode = dr["PromotionThemeCode"].ToString();
                    mainInfo.PromotionPeriodCode = dr["PromotionPeriodCode"].ToString();
                    mainInfo.SubID = int.Parse(dr["SubID"].ToString());
                    mainInfo.CategoryCode = dr["CategoryCode"].ToString();
                    mainInfo.BudgetLine = double.Parse(dr["BudgetLine"].ToString());
                    mainInfo.PriceChangeCauseSKU = CreateDeepCopy(info);
                    mainInfo.PriceChangeCauseSKUList.Add(info);
                    resultList.Add(mainInfo);
                }

                #region 导入数据自身去重
                for (int i = 0; i < resultList.Count; i++)
                {
                    for (int j = resultList.Count - 1; j > i; j--)
                    {
                        if (
                            resultList[i].PromotionThemeCode == resultList[j].PromotionThemeCode
                            && resultList[i].PromotionPeriodCode == resultList[j].PromotionPeriodCode
                            && resultList[i].SubID == resultList[j].SubID
                            && resultList[i].CategoryCode == resultList[j].CategoryCode
                            && resultList[i].PriceChangeCauseSKU.ChangeCauseCode == resultList[j].PriceChangeCauseSKU.ChangeCauseCode
                            )
                        {
                            if (i != j)
                            {
                                resultList.Remove(resultList[j]);
                                errorInfos.Add("第" + (i + 1) + "行与第" + (j + 1) + "行重复");
                            }
                        }
                    }
                }
                #endregion

                //resultList = PriceChangeSKUManager.LoadNames(dbNode, resultList);//填补/修正 名称信息
                //if (resultList.Count <= 0)
                //{
                //    XtraMessageBox.ShowErrorMessage("没有导入任何数据!");
                //    return;
                //}
                //if (opr.ReturnCode == OperateCodeEnum.Warning)
                //{
                //    XtraMessageBox.ShowErrorMessage("没有导入任何数据!");
                //    return;
                //}

                StringBuilder sb = new StringBuilder();
                sb.Append("导入成功[" + (resultList.Count) + "]行\r\n导入失败[" + (ImportNumber - resultList.Count) + "]行" + "\r\n");
                if (errorInfos.Count > 0)
                {
                    sb.Append("以下数据验证失败:" + "\r\n");
                    foreach (var error in errorInfos)
                    {
                        sb.Append(error + "\r\n");
                    }
                    XtraMessageBox.Show(new OperateReturnInfo() { ReturnCode = OperateCodeEnum.Failed, ReturnInfo = sb.ToString() });
                    return;
                }
                else
                { XtraMessageBox.Show("已成功导入" + ImportNumber + "行"); }

                if (resultList.Count == 0) { return; }
                OperateReturnInfo opr = PriceChangeSKUManager.UpdatePriceChangeSKUInfo(dbNode, resultList);
                if (opr.ReturnCode == OperateCodeEnum.Failed)
                {
                    XtraMessageBox.ShowErrorMessage("导入失败!");
                    return;
                }
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show(ex);
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                gvParent.CloseEditor();
                gvChild.CloseEditor();
                if (bsParent.Count <= 0)
                {
                    XtraMessageBox.ShowWarningMessage("没有可保存的数据!");
                    return;
                }

                foreach (PriceChangeSKUInfo item in bsParent.List)
                {
                    if (item == null) { return; }
                    if (item.CategoryCode == null || item.CategoryCode == "")
                    {
                        XtraMessageBox.ShowWarningMessage("二级品类编码不能为空!");
                        return;
                    }
                    if (item.CategoryName == null || item.CategoryName == "")
                    {
                        XtraMessageBox.ShowWarningMessage("二级品类名称不能为空!");
                        return;
                    }
                    if (item.BudgetLine == null || item.BudgetLine.ToString() == "" || item.BudgetLine == 0)
                    {
                        XtraMessageBox.ShowWarningMessage("预算额度必须大于0!");
                        return;
                    }
                    foreach (PriceChangeCauseSKUInfo ob in item.PriceChangeCauseSKUList)
                    {
                        if (ob.SKULowerLimit > ob.SKUUpperLimit)
                        {
                            XtraMessageBox.ShowWarningMessage("选品SKU数量上限必须大于下限!");
                            gvChild.FocusedColumn = colSKUUpperLimit;
                            gvChild.FocusedRowHandle = int.Parse(ob.ChangeCauseCode) - 1;
                            return;
                        }
                    }
                }
                IList PriceChangeSKUList = bsParent.List as IList;
                OperateReturnInfo opr = PriceChangeSKUManager.UpdatePriceChangeSKUInfo(dbNode, PriceChangeSKUList);
                if (opr.ReturnCode == OperateCodeEnum.Warning)
                {
                    XtraMessageBox.ShowWarningMessage("没有可保存的数据!");
                    return;
                }
                XtraMessageBox.Show(opr);
                gvChild.ShowEditor();
                gvParent.ShowEditor();
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private void btnOption_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gcParent.Focused)
            {
                CustomColumnSelectForm f = new CustomColumnSelectForm(this.gvParent);
                f.ShowDialog();
            }
            else
            {
                CustomColumnSelectForm f = new CustomColumnSelectForm(this.gvChild);
                f.ShowDialog();
            }
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (bsParent.Count > 0)
            {
                if (XtraMessageBox.Show("是否保存当前档期数据?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    btnSave_ItemClick(null, null);
                }
            }
            this.Close();
        }
        #endregion

        #region 联想
        private void associateInitDeptCode()
        {
            associateCategoryCode = new GridAssociate(dbNode, "colCategoryCode", AssociateManager, AssociateModeEnum.GoodsCategoryeyCode, null);
            associateCategoryCode.BaseAssociate.Associated += new AssociateBase.AssociateEventHandler(BaseAssociate_Associated);
            associateCategoryCode.CurrentConditions["categoryitemcode"] = "0000";
            colCategoryCode.ColumnEdit = associateCategoryCode;
            associateCategoryName = new GridAssociate(dbNode, "colCategoryName", AssociateManager, AssociateModeEnum.GoodsCategoryeyName, null);
            associateCategoryName.BaseAssociate.Associated += new AssociateBase.AssociateEventHandler(BaseAssociate_Associated);
            associateCategoryName.CurrentConditions["categoryitemcode"] = "0000";
            colCategoryName.ColumnEdit = associateCategoryName;
        }

        void BaseAssociate_Associated(Bizcent.Client.Common.AssociateControl.AssociateEventArgs e)
        {
            try
            {
                gvParent.CloseEditor();
                if (e.CurrentState == AssociateStateEnum.Normal)
                {
                    PromotionPeriodInfo currentPromotion = bsPromotionPeriod.List[luePromotionPeriod.ItemIndex] as PromotionPeriodInfo;
                    if (luePromotionPeriod.Text == null || luePromotionPeriod.Text == "")
                    {
                        XtraMessageBox.ShowWarningMessage("请先选择档期!");
                        return;
                    }
                    DataRow dr = e.CurrentDataRow;
                    PriceChangeSKUInfo currentRow = bsParent.Current as PriceChangeSKUInfo;
                    currentRow.PromotionThemeCode = currentPromotion.PromotionThemeCode;
                    currentRow.PromotionPeriodCode = currentPromotion.PromotionPeriodCode;
                    currentRow.SubID = currentPromotion.SubID;
                    currentRow.CategoryCode = dr[0].ToString();
                    currentRow.CategoryName = dr[1].ToString();
                    if (currentRow == null) { return; }
                    int sameNumPanel = 0;
                    foreach (PriceChangeSKUInfo item in bsParent.List)
                    {
                        if (item.CategoryCode == currentRow.CategoryCode)
                        {
                            sameNumPanel++;
                        }
                    }
                    if (sameNumPanel > 1)
                    {
                        XtraMessageBox.ShowWarningMessage("二次品类编码已存在!");
                        currentRow.CategoryCode = "";
                        currentRow.CategoryName = "";
                        bsParent.ResetBindings(false);
                        gvParent.FocusedColumn = colCategoryCode;
                        return;
                    }
                    IList childList = PriceChangeSKUManager.LoadChild(dbNode, currentRow);
                    currentRow.PriceChangeCauseSKUList = childList;
                    bsChild.DataSource = childList;
                    bsChild.ResetBindings(false);
                    associateCategoryCode.CurrentValue = dr[0].ToString();
                    associateCategoryName.CurrentValue = dr[1].ToString();
                    bsParent.ResetBindings(false);
                    gvParent.FocusedColumn = colBudgetLine;
                    gvParent.ShowEditor();
                }
            }
            catch (Exception ex)
            { XtraMessageBox.Show(ex); }
        }
        #endregion

        #region 表格和控件事件
        private void luePromotionPeriod_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (bsParent.Count > 0)
                {

                    if (XtraMessageBox.Show("是否保存当前档期数据?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        btnSave_ItemClick(null, null);
                    }
                    PromotionPeriodInfo currentRow = bsPromotionPeriod.List[luePromotionPeriod.ItemIndex] as PromotionPeriodInfo;
                    deStart.Text = currentRow.BeginDate;
                    deEnd.Text = currentRow.EndDate;
                    IList parentList = PriceChangeSKUManager.LoadCategory(dbNode, currentRow);
                    if (parentList.Count == 0) { bsChild.Clear(); }
                    foreach (PriceChangeSKUInfo item in parentList)
                    {
                        item.PromotionThemeCode = currentRow.PromotionThemeCode;
                        item.PromotionPeriodCode = currentRow.PromotionPeriodCode;
                        item.SubID = currentRow.SubID;
                    }
                    bsParent.DataSource = parentList;
                    bsParent.ResetBindings(false);
                    if (parentList.Count > 0)
                    {
                        IList childList = PriceChangeSKUManager.LoadChild(dbNode, parentList[0]);
                        parentList[0].PriceChangeCauseSKUList = childList;
                        bsChild.DataSource = parentList[0].PriceChangeCauseSKUList;
                    }

                }
                else
                {
                    PromotionPeriodInfo currentRow = bsPromotionPeriod.List[luePromotionPeriod.ItemIndex] as PromotionPeriodInfo;
                    deStart.Text = currentRow.BeginDate;
                    deEnd.Text = currentRow.EndDate;
                    IList parentList = PriceChangeSKUManager.LoadCategory(dbNode, currentRow);
                    if (parentList.Count == 0) { bsChild.Clear(); }
                    foreach (PriceChangeSKUInfo item in parentList)
                    {
                        item.PromotionThemeCode = currentRow.PromotionThemeCode;
                        item.PromotionPeriodCode = currentRow.PromotionPeriodCode;
                        item.SubID = currentRow.SubID;
                    }
                    bsParent.DataSource = parentList;
                    bsParent.ResetBindings(false);
                    if (parentList.Count > 0)
                    {
                        IList childList = PriceChangeSKUManager.LoadChild(dbNode, parentList[0]);
                        parentList[0].PriceChangeCauseSKUList = childList;
                        bsChild.DataSource = parentList[0].PriceChangeCauseSKUList;
                    }
                }
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show(ex);
            }
        }

        private void gvParent_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                gvParent.CloseEditor();
                gvChild.CloseEditor();
                PriceChangeSKUInfo currentRow = bsParent.Current as PriceChangeSKUInfo;
                if (currentRow == null) { return; }
                PromotionPeriodInfo PromotionPeriod = bsPromotionPeriod.List[luePromotionPeriod.ItemIndex] as PromotionPeriodInfo;
                currentRow.PromotionThemeCode = PromotionPeriod.PromotionThemeCode;
                currentRow.PromotionPeriodCode = PromotionPeriod.PromotionPeriodCode;
                currentRow.SubID = PromotionPeriod.SubID;
                if (e.PrevFocusedRowHandle != GridControl.InvalidRowHandle && bsParent.Current != null
                     && bsParent.Count != 0 && bsParent.Count > e.PrevFocusedRowHandle)
                {
                    PriceChangeSKUInfo previousRow = bsParent[e.PrevFocusedRowHandle] as PriceChangeSKUInfo;
                    if (previousRow == null) { return; }
                    if (string.IsNullOrEmpty(previousRow.CategoryCode) && string.IsNullOrEmpty(previousRow.CategoryName))
                    {
                        bsParent.Remove(previousRow);
                        if (currentRow.CategoryCode != "")
                        {
                            if (currentRow.PriceChangeCauseSKUList.Count <= 0)
                            {
                                IList childList = PriceChangeSKUManager.LoadChild(dbNode, currentRow);
                                currentRow.PriceChangeCauseSKUList = childList;
                                bsChild.DataSource = currentRow.PriceChangeCauseSKUList;
                            }
                            else
                            {
                                bsChild.DataSource = currentRow.PriceChangeCauseSKUList;
                            }
                        }
                    }
                    else
                    {
                        if (currentRow.CategoryCode != "")
                        {
                            if (currentRow.PriceChangeCauseSKUList.Count <= 0)
                            {
                                IList childList = PriceChangeSKUManager.LoadChild(dbNode, currentRow);
                                currentRow.PriceChangeCauseSKUList = childList;
                                bsChild.DataSource = currentRow.PriceChangeCauseSKUList;
                            }
                            else
                            {
                                bsChild.DataSource = currentRow.PriceChangeCauseSKUList;
                            }
                        }
                        else
                        {
                            bsChild.DataSource = currentRow.PriceChangeCauseSKUList;
                            bsChild.ResetBindings(false);
                        }
                    }
                    //if (previousRow.CategoryCode == null || previousRow.CategoryCode == "")
                    //{
                    //    XtraMessageBox.ShowWarningMessage("二级品类编码不能为空!");
                    //    gvParent.FocusedRowHandle = e.PrevFocusedRowHandle;
                    //    return;
                    //}
                    //if (previousRow.CategoryName == null || previousRow.CategoryName == "")
                    //{
                    //    XtraMessageBox.ShowWarningMessage("二级品类名称不能为空!");
                    //    gvParent.FocusedRowHandle = e.PrevFocusedRowHandle;
                    //    return;
                    //}
                    //IList tempList = bsChild.List as IList;
                    //if (tempList == null) { return; }
                    //foreach (PriceChangeCauseSKUInfo item in tempList)
                    //{
                    //    if (item.SKUUpperLimit < item.SKULowerLimit)
                    //    {
                    //        XtraMessageBox.ShowWarningMessage("选品SKU数量上限必须大于下限!");
                    //        gvParent.FocusedRowHandle = e.PrevFocusedRowHandle;
                    //        return;
                    //    }
                    //}
                    //for (int i = 0; i < tempList.Count; i++)
                    //{
                    //    PriceChangeCauseSKUInfo tempInfo = new PriceChangeCauseSKUInfo();
                    //    tempInfo = CreateDeepCopy(tempList[i]);
                    //    previousRow.PriceChangeCauseSKUList.Add(tempInfo);
                    //}
                    //bsParent.ResetBindings(false);
                    //if (currentRow.CategoryCode != "")
                    //{
                    //    if (currentRow.PriceChangeCauseSKUList.Count != 0)
                    //    {
                    //        bsChild.DataSource = currentRow.PriceChangeCauseSKUList;
                    //    }
                    //    else
                    //    {
                    //        LoadandBoundChildData(currentRow);
                    //    }
                    //}
                    //else
                    //{ bsChild.Clear(); }
                }
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show(ex);
            }
        }

        private void gvParent_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                gvParent.CloseEditor();
                if (e.KeyCode == Keys.Down)
                {
                    if (gvParent.FocusedRowHandle == bsParent.Count - 1)
                    {
                        btnAddRow_ItemClick(null, null);
                    }
                }
                else if (e.KeyCode == Keys.Enter)
                {
                    PriceChangeSKUInfo currentRow = bsParent.Current as PriceChangeSKUInfo;
                    if (gvParent.FocusedColumn == colBudgetLine && currentRow.BudgetLine != 0 && currentRow.BudgetLine.ToString() != ""
                        && currentRow.CategoryCode != "" && currentRow.CategoryName != "")
                    {
                        btnAddRow_ItemClick(null, null);
                    }
                }
                gvParent.ShowEditor();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex);
            }

        }

        private void gvParent_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                gvParent.CloseEditor();
                GridHitInfo hInfo = gvParent.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.HitTest.ToString() == "EmptyRow")
                {
                    btnAddRow_ItemClick(null, null);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex);
            }
        }

        private void repositoryItemTextEdit4_EditValueChanged(object sender, EventArgs e)
        {


        }

        private void gvChild_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                gvChild.CloseEditor();
                PriceChangeCauseSKUInfo currentRow = bsChild.Current as PriceChangeCauseSKUInfo;
                if (currentRow == null) { return; }
                if (currentRow.SKULowerLimit.ToString() != "" && currentRow.SKUUpperLimit.ToString() != "" && currentRow.SKUUpperLimit != 0)
                {
                    if (currentRow.SKULowerLimit > currentRow.SKUUpperLimit)
                    {
                        XtraMessageBox.ShowWarningMessage("选品SKU数量上限必须大于下限!");
                        return;
                    }
                }
                gvChild.ShowEditor();
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show(ex);
            }
        }

        private void gvChild_KeyDown(object sender, KeyEventArgs e)
        {
            gvChild.CloseEditor();
            if (e.KeyCode == Keys.Enter)
            {
                if (gvChild.FocusedColumn == colPlanSKUNum)
                {
                    gvChild.FocusedColumn = colSKULowerLimit;
                    return;
                }
                if (gvChild.FocusedColumn == colSKULowerLimit)
                {
                    gvChild.FocusedColumn = colSKUUpperLimit;
                    return;
                }
                PriceChangeCauseSKUInfo currentRow = bsChild.Current as PriceChangeCauseSKUInfo;
                if (gvChild.FocusedColumn == colSKUUpperLimit && currentRow.PlanSKUNum.ToString() != "" && currentRow.SKULowerLimit.ToString() != ""
                && currentRow.SKUUpperLimit.ToString() != "" && currentRow.SKUUpperLimit >= currentRow.SKULowerLimit)
                {
                    gvChild.MoveNext();
                    gvChild.FocusedColumn = colPlanSKUNum;
                }
            }
            gvChild.ShowEditor();
        }
        #endregion
    }
}

你可能感兴趣的:(双gridview联动模块)