ComboboxEdit控件的使用

来源:互联网 发布:mac nose for style 编辑:程序博客网 时间:2024/06/06 10:03
ComboboxEdit 绑定数据及显示:

1.定义一个类,方便添加数据
publicclassExComboBox
    {
       publicintIndex {get;set; }
       publicstringKey {get;set; }
       publicstringValue {get;set; }
       publicstringTag {get;set; }
       publicExComboBox(intpIndex,stringpKey,stringpValue)
        {
           this.Index = pIndex;
           this.Key = pKey;
           this.Value = pValue;
        }
       publicExComboBox(intpIndex,stringpKey,stringpValue,stringpTag)
        {
           this.Index = pIndex;
           this.Key = pKey;
           this.Value = pValue;
           this.Tag = pTag;
        }
       publicoverridestringToString()
        {
           returnthis.Value;
        }
    }     //此类宜放置在界面函数外,命名空间内,最后一行。

2.再写绑定数据过程方法
       publicvoidBindComboBoxEdit(DevExpress.XtraEditors.ComboBoxEditcmb,List<AProductsInfo> editvalue)
        {
            cmb.Properties.Items.Clear();
           for(inti = 0; i < editvalue.Count; i++)
            {
               AProductsInfoinfo = editvalue[i];
               ExComboBoxexValue =newExComboBox(i, info.ID.ToString(), info.Prod_name);
                cmb.Properties.Items.Add(exValue);
            }
           if(cmb.Properties.Items.Count > 0)
                cmb.SelectedIndex = 0;
        }                        //方法写在界面函数内任意位置

3.绑定数据应在界面加载后进行,页面load属性里面加,WHC框架提供了界面初始化代码位置:private void InitDictItem()

   进行绑定:
      privatevoidInitDictItem()
             {
                //初始化代码
                List<ACustomersInfo> infoList = BLLFactory<ACustomers>.Instance.GetAll();
                 BindComboBoxEdit(txtCustomersID,infoList);
             }                //表内信息Info和方法集,下面是使用规定好的绑定过程



ComboboxEdit 查询条件的转义:     

1.原查询方法。
     由于原查询为ID查询,现在要改成,先由下拉表信息查询相关表中的ID,再传回ID,执行原来的方法。
      privatestringGetConditionSql()
        {
           //如果存在高级查询对象信息,则使用高级查询条件,否则使用主表条件查询
           SearchConditioncondition = advanceCondition;
           if(condition ==null)
            {
                condition =newSearchCondition();
                condition.AddCondition("VendorsID",this.txtVendorsID.Text.Trim(),SqlOperator.Like);   
                condition.AddCondition("Prod_name",this.txtProd_name.Text.Trim(),SqlOperator.Like);
            }
           stringwhere = condition.BuildConditionSql().Replace("Where","");
           returnwhere;
        }
        
       privatevoidBindData()
        {
              //entity
           this.winGridViewPager1.DisplayColumns ="ID,VendorsID,prod_name,prod_price,prod_desc";
           this.winGridViewPager1.ColumnNameAlias =BLLFactory<AProducts>.Instance.GetColumnNameAlias();//字段列显示名称转义

           stringwhere = GetConditionSql1();
                  List<AProductsInfo> list = BLLFactory<AProducts>.Instance.FindWithPager(where,this.winGridViewPager1.PagerInfo);
           this.winGridViewPager1.DataSource = list;//new WHC.Pager.WinControl.SortableBindingList<AProductsInfo>(list);
               this.winGridViewPager1.PrintTitle ="AProducts报表";
         }

2.获取下拉框数据,准备条件。
  按步骤,首先在下拉框中获取显示数据,转换成条件,为下一步在相关表查找所需值做准备。
       privatestringAcquireName()
        {
           //获取下拉表名字,返回条件语句
           SearchConditioncondition = advanceCondition;
           if(condition ==null)
            {
                condition =newSearchCondition();
                condition.AddCondition("vend_name",this.txtVendorsID.Text.Trim(),SqlOperator.Like);
            }
           stringwhere = condition.BuildConditionSql().Replace("Where","");
           returnwhere;
        }

3.相关表查找出所需值(ID)。
  步骤继续,接上一步获得查询条件,在相关表查询对应的所需值。
     
      privateList<string> FindID()
        {
           //查询对应表ID,返回所需值
           List<string> Need = new List<string>();
           //List<AVendorsInfo> dt = BLLFactory<AVendors>.Instance.GetAll();
           stringwhere = AcquireName();
           List<AVendorsInfo> list = BLLFactory<AVendors>.Instance.Find(where);
           for(inti = 0; i < list.Count; i++)
            {
               AVendorsInfoinfo = list[i];
                Need.Add(info.ID.ToString());
            }
           returnNeed;
        }

4.修改界面查询方法。
  由于原查询方法中是直接取下拉框数据查询,此处修改为之前两步查到的所需值(ID)来查找。
           
       privatestringGetConditionSql()
        {
           //如果存在高级查询对象信息,则使用高级查询条件,否则使用主表条件查询
           List<string> Need = null;
           SearchConditioncondition = advanceCondition;
           if(condition ==null)
            {
                condition =newSearchCondition();
                Need = FindID();
               for(inti = 0; i < Need.Count; i++)
                {
                    condition.AddCondition("VendorsID", Need[i], SqlOperator.Equal);
                }                         
             //把以上每个获取的所需值(ID)都用查询方法添加查询语句到where中去。
                    condition.AddCondition("Prod_name",this.txtProd_name.Text.Trim(),SqlOperator.Like);
            }
           stringwhere = condition.BuildConditionSql().Replace("Where","");
           returnwhere;
        }

修改完成。
结论: 由于框架所生成的界面一般是普通输入编辑框,且只能生成一个表中的查询功能,此处我所做的工作为:
               1.生成下拉框comboboxEdit,绑定数据。
                    实现了,下拉框显示数据,并且可以显示用所生成的表之外的,相关表中的数据。
                    (例:此界面为Products,中有一个经销商ID,我要显示的是经销商名称,products表中没有经销商名称。则,绑定数据时,代码层到Vendors表中用Produccts表中的VendorsID,查询vend_name来显示到下拉框中。)


                2.选择下拉框数据(来源外表)查询,查询条件转换成此表。
                    实现了用户界面友好的数据显示,而查询中用到的ID等不方便数据,直接在代码层面转义查询。

ComboboxEdit 添加数据的转义:  
1.同上述步骤2,获取下拉框数据作为条件,准备到外表中查找所需值。
          privatestringAcquireName();
             
//需要注意的是,这里的数据库操作不同于模糊查询用LIKE方法,必须要用Equal,因为选择名不会出错,并且添加数据不能为多行。
//编辑界面中可能缺少一些方法的调用,此处加一个条件方法   
                    privateSearchConditionadvanceCondition;

2.同上述步骤3,到相关表查出所需值(ID)
          privateList<string> FindID()

3.不同于上述查询方法,此处修改添加数据代码:SetInfo的步骤
          
       privatevoidSetInfo(AOrdersInfoinfo)
        {
           intNeed = FindID();               //因为之前所需值很多必须为List型,现在只需int型即可
                  //info.CustomersID = txtCustomersID.Text.ToInt32();   此为之前原有添加数据来源
                  info.CustomersID = Need;
                   info.Order_date = txtOrder_date.DateTime;
           }
0 0
原创粉丝点击