ComboboxEdit控件的使用

本文介绍了在Winform中如何使用ComboboxEdit控件进行数据绑定和查询条件转换。通过创建自定义类ExComboBox,实现数据绑定,然后在界面加载后进行绑定。对于查询条件的转义,详细阐述了从下拉框获取数据,转换为查询条件,再到相关表查询所需值的过程,最后修改查询方法,以匹配下拉框选择的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ComboboxEdit 绑定数据及显示:

1.定义一个类,方便添加数据
public class ExComboBox
    {
        public int Index { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }
        public string Tag { get; set; }
        public ExComboBox(int pIndex, string pKey, string pValue)
        {
            this.Index = pIndex;
            this.Key = pKey;
            this.Value = pValue;
        }
        public ExComboBox(int pIndex, string pKey, string pValue, string pTag)
        {
            this.Index = pIndex;
            this.Key = pKey;
            this.Value = pValue;
            this.Tag = pTag;
        }
        public override string ToString()
        {
            return this.Value;
        }
    }     //此类宜放置在界面函数外,命名空间内,最后一行。

2.再写绑定数据过程方法
        public void BindComboBoxEdit(DevExpress.XtraEditors.ComboBoxEdit cmb, List<AProductsInfo> editvalue)
        {
            cmb.Properties.Items.Clear();
            for (int i = 0; i < editvalue.Count; i++)
            {
                AProductsInfo info = editvalue[i];
                ExComboBox exValue = new ExComboBox(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()

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



ComboboxEdit 查询条件的转义:     

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

            string where = 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.获取下拉框数据,准备条件。
  按步骤,首先在下拉框中获取显示数据,转换成条件,为下一步在相关表查找所需值做准备。
        private string AcquireName()
        {
            //获取下拉表名字,返回条件语句
            SearchCondition condition = advanceCondition;
            if (condition == null)
            {
                condition = new SearchCondition();
                condition.AddCondition("vend_name", this.txtVendorsID.Text.Trim(), SqlOperator.Like);
            }
            string where = condition.BuildConditionSql().Replace("Where", "");
            return where;
        }

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

4.修改界面查询方法。
  由于原查询方法中是直接取下拉框数据查询,此处修改为之前两步查到的所需值(ID)来查找。
           
        private string GetConditionSql()
        {
            //如果存在高级查询对象信息,则使用高级查询条件,否则使用主表条件查询
            List<string> Need = null;
            SearchCondition condition = advanceCondition;
            if (condition == null)
            {
                condition = new SearchCondition();
                Need = FindID();
                for (int i = 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);
            }
            string where = condition.BuildConditionSql().Replace("Where", "");
            return where;
        }

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


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值