zoukankan      html  css  js  c++  java
  • 泛型与反射机制处理ComBox绑定的数据源是List<实体对象>

     今天在做把传统的ComBox绑定的数据源是List<实体对象>,有很多的页面都是这么处理的,而且实体类不同.

    心想不能每个页面都吧,后来就想着用泛型与反射机制处理:才写出了这个通用的方法如下:

    这可以偶花了好几个小时的心血哦,有需要的直接拿去用吧。而且以这个为例也可变化出很多的用法。大家快来学习一下吧!

      #region public static void SetComboBoxData(DevExpress.XtraEditors.ImageComboBoxEdit comboBox,List<T> list, string valueMember, string displayMember, string selectedText = null)
            /// <summary>
            /// 绑定下拉框
            /// </summary>
            /// <param name="comboBox">下拉控件</param>
            /// <param name="List<T> ">实体集合</param>
            /// <param name="valueMember">值字段</param>
            /// <param name="displayMember">显示字段</param>
            /// <param name="selectedText">默认选中的值</param>
            public static void SetComboBoxData<T>(DevExpress.XtraEditors.ImageComboBoxEdit comboBox, List<T> list, string valueMember, string displayMember, string selectedText = null)//
            {
                comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
                comboBox.Properties.NullText = string.Empty;
            
                foreach (var item in list)
                {
                    comboBox.Properties.Items.Add(new ImageComboBoxItem(item.GetType().GetProperty(valueMember).GetValue(item, null).ToString(), item.GetType().GetProperty(valueMember).GetValue(item, null).ToString()));
                }

                // //这里是设置默认选中的值
                if (!string.IsNullOrEmpty(selectedText))
                {
                    comboBox.SelectedItem = comboBox.Properties.Items.GetItem(selectedText);
                }
            }
            #endregion

  • 相关阅读:
    opentsdb安装部署
    python发送邮件(html)例子
    python查库写库例子
    获取rds的cpu和内存使用情况
    数据库损坏的情况下如何获取到dbid
    grafana安装升级部署
    Specified key was too long
    mysql动态执行sql批量删除数据
    kafka删除topics
    cratedb导入json文件
  • 原文地址:https://www.cnblogs.com/spring_wang/p/3072640.html
Copyright © 2011-2022 走看看