首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于Combox简单赋值的有关问题

2013-01-20 
关于Combox简单赋值的问题 /// summary/// 根据数据类型返回加载后的ComboBox/// /summary/// param

关于Combox简单赋值的问题


 /// <summary>
        /// 根据数据类型  返回加载后的ComboBox
        /// </summary>
        /// <param name="type">分为字符型T 数字型N 时间型D 是否型B</param>
        /// <returns></returns>
        private ComboBox GetOperateCombox(string type)
        {
            try
            {
                if (dtOperate == null)
                {
                    dtOperate = Qport.Framework.DBAccess.DataAccess.Instance(QportGlobalParam.strStatDB).SelectDataTable("select * from stat_operate_define");
                }
                ComboBox cb = new ComboBox();
                cb.DropDownStyle = ComboBoxStyle.DropDownList;
                cb.DisplayMember = "operate_disp_name";
                cb.ValueMember = "operate_start_code";
                string strType;
                if (type == "N")
                {
                    strType = "num";
                }
                else if (type == "D")
                {
                    strType = "datetime";
                }
                else if (type == "B")
                {
                    strType = "bool";
                }
                else
                {
                    strType = "str";
                }


                DataRow[] dr = dtOperate.Select("field_type='" + strType + "'");
                //foreach (DataRow item in dr)
                //{
                 //   cb.Items.Add(item);
                //}
                //cb.DataSoure = dr;
                return cb;
            }
            catch (Exception ex)
            {
                Qport.Framework.LogHandle.QportLogger.Instance().LogException(ex);
                return null;
            }

        }


如果用dataTable赋值  需要从数据库中查多次
但还想用displayMember  与 ValueMember这种方式赋值 必竟取值时方便
但不用dataTable还有其它能绑定上的方法吗?
[解决办法]
一次性 把所有 要绑定到ComboBox的全部查询到DataSet中

写一个函数:
void BindComBox(ComboBox comboBox, DataTable source,string displayMember,string valueMember)
{
   绑定数据,控件、数据源、显示列,值列 对号入座。
}

一次性把数据源查询到DataSet中,然后调用BindComBox,对号入座。
 仅供参考。
 
[解决办法]
查询和对控件赋值又不关联的,
你可以先把数据查出来生成DataTable放到内存中,然后绑定啊,手动赋值随你便了
[解决办法]

 /// <summary>
        /// 根据数据类型  返回加载后的ComboBox
        /// </summary>
        /// <param name="type">分为字符型T 数字型N 时间型D 是否型B</param>
        /// <returns></returns>
        private ComboBox GetOperateCombox(string type)
        {
            try
            {
                if (dtOperate == null)
                {
                    dtOperate = Qport.Framework.DBAccess.DataAccess.Instance(QportGlobalParam.strStatDB).SelectDataTable("select * from stat_operate_define");
                }
                ComboBox cb = new ComboBox();


                cb.DropDownStyle = ComboBoxStyle.DropDownList;
                cb.DisplayMember = "operate_disp_name";
                cb.ValueMember = "operate_start_code";
                string strType;
                if (type == "N")
                {
                    strType = "num";
                }
                else if (type == "D")
                {
                    strType = "datetime";
                }
                else if (type == "B")
                {
                    strType = "bool";
                }
                else
                {
                    strType = "str";
                }
            DataView dvOperate = new DataView(dtOperate);
            dvOperate.RowFilter = "field_type='" + strType + "'";
            cb.DataSource=dvOperate;
                return cb;
            }
            catch (Exception ex)
            {
                Qport.Framework.LogHandle.QportLogger.Instance().LogException(ex);
                return null;
            }
 
        }

热点排行