Grid和TreeList组合使用.

Posted on 2007-06-01 11:17  xzc5858  阅读(1446)  评论(0编辑  收藏  举报

     初始化:
         private DataView recordTenementChargeMenus = null;
         public DataTable dt1;
        public TenementChargeSetForm()
        {
            InitializeComponent();
            this.recordTenementChargeMenus = null;
            this.gridView.LayoutName = "PartInForm";

            this.gridView.ExOptionsView.ShowAddDelMenu = true;
            this.gridView.OptionsBehavior.Editable =true;
            this.gridView.OptionsBehavior.EditorShowMode = EditorShowMode.Default;
            this.gridView.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            this.BindData();
        }
建表一个表,然后绑定其到TreeList中,
        protected  void BindData()
        {
            
            dt1.Columns.Add("id", typeof(int));
            dt1.Columns.Add("parentid", typeof(int));
            dt1.Columns.Add("treename", typeof(string));
            DataTable oldDT = new DataTable();
            oldDT = BaseDB.Linker.ExecuteDataTable("select * from t_ChargeModel");
            for (int i = 0; i < oldDT.Rows.Count; i++)
            {
                DataRow dr = dt1.NewRow();
                dr["id"] = oldDT.Rows[i]["chargeID"];
                dr["treename"] = oldDT.Rows[i]["chargeModelName"];
                dt1.Rows.Add(dr);
            }

            DataTable OoldDt = new DataTable();
            OoldDt = BaseDB.Linker.ExecuteDataTable("select * from t_ChargeItem");
            for (int i = 0; i < OoldDt.Rows.Count; i++)
            {
                DataRow dr = dt1.NewRow();
                dr["id"] = OoldDt.Rows[i]["ChargeItemCodeID"];
                dr["parentid"] = OoldDt.Rows[i]["chargeID"];
                dr["treename"] = OoldDt.Rows[i]["ChargeName"];
                dt1.Rows.Add(dr);
            }

            exTreeList1.DataSource = dt1;
            exTreeList1.ParentFieldName = "parentid";
            exTreeList1.KeyFieldName = "id";
            exTreeList1.ExpandAll();

        }


        public override void btnSave_ItemClick(object sender, ItemClickEventArgs e)
        {

            base.btnSave_ItemClick(sender, e);

                DevExpress.XtraTreeList.TreeListMultiSelection mMSTN = this.exTreeList1.Selection;
                for (int i = 0; i < mMSTN.Count; i++)
                {
                    string st = mMSTN[i][this.exTreeList1.KeyFieldName].ToString();
                    if (mMSTN[i][this.exTreeList1.ParentFieldName].ToString() != string.Empty)
                    {

                        this.SaveGroupPower(st);
                    }
                    else
                    {
                        MessageUtil.ShowError("请选择收费项目具体项目");
                    }
                }

     


        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            this.SetPowerValue("1");
        }

               private void btnClear_Click(object sender, EventArgs e)
        {
            this.SetPowerValue("0");
        }

        private void exTreeList1_AfterFocusNode(object sender, NodeEventArgs e)
        {

            if (e.Node != null)
            {
                DevExpress.XtraTreeList.TreeListMultiSelection mMSTN = this.exTreeList1.Selection;
                for (int i = 0; i < mMSTN.Count; i++)
                {
                    string st = mMSTN[i][this.exTreeList1.KeyFieldName].ToString();
                    if (mMSTN[i][this.exTreeList1.ParentFieldName].ToString() != string.Empty)
                    {
                        recordTenementChargeMenus = PowerWS.GetTenementCharge(st);
                        gridControl.DataSource = recordTenementChargeMenus;

                    }
                }

            }

        }

        public bool HaveModuleFunc(int iValue, int iMenuFunc)
        {
            return ((iValue & iMenuFunc) == iValue);
        }
        public void SaveGroupPower(string st)
        {
            if ((this.recordTenementChargeMenus != null) && (null != st))
            {

                string sSql = "";
                BaseBizDB.Linker.ClearSqlByTrans();
                this.recordTenementChargeMenus.GoTop();
                while (!this.recordTenementChargeMenus.Eof())
                {
                        DataRow dr;
                        if (this.recordTenementChargeMenus.GetLong("NewMenu") == 1)
                        {
                            if (this.recordTenementChargeMenus.GetLong("Inserted") == 1)
                            {
                                if (this.recordTenementChargeMenus.GetInt("HaveCharge")==1)
                                {
                                dr = this.recordTenementChargeMenus.Table.Rows[this.recordTenementChargeMenus.CurrentPos];
                                sSql = string.Concat(new object[] { "INSERT INTO t_ChargeList(ChargeItemCodeID, chargelistName, HouseCodeID) VALUES(" + st+ ",'" + this.recordTenementChargeMenus.GetString("HouseName") + "'," + this.recordTenementChargeMenus.GetString("HouseCodeID") + ")" });
                                BaseBizDB.Linker.AppendSqlByTrans(sSql);
                            }}
                        }
   
                        else if (this.recordTenementChargeMenus.GetLong("Modified") == 1)
                        {
                            if (this.recordTenementChargeMenus.GetInt("HaveCharge") == 0)
                            {
                            dr = this.recordTenementChargeMenus.Table.Rows[this.recordTenementChargeMenus.CurrentPos];
                            sSql = string.Concat(new object[] { "DELETE  from t_ChargeList where HouseCodeID=" + this.recordTenementChargeMenus.GetString("HouseCodeID") + " and  ChargeItemCodeID=" + st + "" });
                            BaseBizDB.Linker.AppendSqlByTrans(sSql);
                            }
                        }
                        this.recordTenementChargeMenus.Skip();
                  
                }
                int iRet = BaseBizDB.Linker.ExecuteSqlCollectionByTrans(false);
                this.ExecuteHint(iRet, BaseBizDB.Linker.ErrString);
                if (iRet >= 0)
                {
                    this.recordTenementChargeMenus.GoTop();
                    while (!this.recordTenementChargeMenus.Eof())
                    {
                        if (this.recordTenementChargeMenus.GetLong("Inserted") == 1)
                        {
                            this.recordTenementChargeMenus.SetLong("Inserted", (long)0);
                            this.recordTenementChargeMenus.SetLong("NewMenu", (long)0);
                        }
                        if (this.recordTenementChargeMenus.GetLong("ExecBrowser") == 0)
                        {
                            this.recordTenementChargeMenus.SetLong("Inserted", (long)1);
                            this.recordTenementChargeMenus.SetLong("NewMenu", (long)1);
                        }
                        this.recordTenementChargeMenus.SetLong("Modified", (long)0);
                        this.recordTenementChargeMenus.Skip();
                    }
                    this.recordTenementChargeMenus.AcceptChanges();
                    MessageUtil.ShowInfo("保存成功");
                    DevExpress.XtraTreeList.TreeListMultiSelection mMSTN = this.exTreeList1.Selection;
         
                            recordTenementChargeMenus = PowerWS.GetTenementCharge(st);
                            gridControl.DataSource = recordTenementChargeMenus;

                }
            }
        }

        public void SetPowerValue(string sValue)
        {
            if (this.recordTenementChargeMenus != null)
            {
                try
                {
                    this.gridView.BeginDataUpdate();
                    for (int i = 0; i < this.recordTenementChargeMenus.Count; i++)
                    {
                        //int iMenuFunc = ConvertUtil.GetDBInt(this.recordTenementChargeMenus[i]["chargelistCodeID"]);
                        this.recordTenementChargeMenus[i]["HaveCharge"] = sValue;
                        this.SetMenuState(this.recordTenementChargeMenus[i].Row);
                    }
                }
                finally
                {
                    this.gridView.EndDataUpdate();
                }
            }
        }


        public void SetMenuState(DataRow dr)
        {
            if ((dr != null) && (ConvertUtil.GetDBInt(dr["Inserted"]) != 1))//说明不是新增加
            {
                if (ConvertUtil.GetDBInt(dr["NewMenu"]) == 1)
                {
                    dr["Inserted"] = 1;//新增数据
                }
                else
                {
                    dr["Modified"] = 1;//已经有该条数据,为修改,则要标记该条数据,要删除.
                }
            }
        }

        private void itemChkExec_EditValueChanged(object sender, EventArgs e)
        {
            DataRow dr = this.gridView.GetDataRow(this.gridView.FocusedRowHandle);
            if ((dr != null) && (this.gridView.FocusedColumn != null))
            {
                //MessageBox.Show(dr[this.gridView.FocusedColumn.FieldName].ToString());
                string strA = dr[this.gridView.FocusedColumn.FieldName].ToString();//获取选取的是哪个
                string strB = ((BaseEdit)sender).EditValue.ToString();//
                if (string.Compare(strA, strB, true) != 0)
                {
                    dr[this.gridView.FocusedColumn.FieldName] = strB;
                    this.SetMenuState(dr);
                }
            }
        }

        private void gridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            //如果不是主项,则不显示文字.也就是要求选择框不要显示文字.
            DataRow dr = this.gridView.GetDataRow(e.RowHandle);
            if ((((e.Column != null) && (null != dr)) && ((e.Column.FieldName != "HouseName") && (e.Column.FieldName != "FloorName") && (e.Column.FieldName != "BuildingName"))) && !this.CanShowEditor(e.Column, dr))
            {
        
                e.DisplayText = "";
            }

        }

        private void gridView_ShowingEditor(object sender, CancelEventArgs e)
        {
            DataRow dr = this.gridView.GetDataRow(this.gridView.FocusedRowHandle);
            if ((this.gridView.FocusedColumn != null) && (null != dr))
            {
                e.Cancel = !this.CanShowEditor(this.gridView.FocusedColumn, dr);
            }
        }

        private void gridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
        {
            ////设置某个列显示为选择框.
            DataRow dr = this.gridView.GetDataRow(e.RowHandle);
            if (((e.Column != null) && (null != dr)) && this.CanShowEditor(e.Column, dr))
            {
                e.RepositoryItem = this.itemChkExec;
            }
        }

        private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            //设定列的色彩
            DataRow dr = this.gridView.GetDataRow(e.RowHandle);
            if ((e.Column != null) && (null != dr))
            {
                if (e.Column.FieldName == "HouseName")
                {
                    e.Appearance.BackColor = Color.AntiqueWhite;
                }
                else if (this.CanShowEditor(e.Column, dr))
                {
                    e.Appearance.BackColor = Color.LemonChiffon;
                }
                else
                {
                    e.Appearance.BackColor = Color.FromArgb(0xe0, 0xe0, 0xe0);
                }
            }
        }

        private bool CanShowEditor(GridColumn col, DataRow dr)
        {
            if (((dr != null) && (col != null)) && (col.Tag != null))
            {
                //这个本来是控制菜单是否显示选择框的.通过菜单表确定的.
                            return true;

            }
            return false;
        }

Copyright © 2024 xzc5858
Powered by .NET 8.0 on Kubernetes