C# 自定义控件字段绑定示例

自定义控件的编辑框引用自Devexpress的TextEdit控件.定义了两个DataSet类型,一个存放记录数据源,一个存放字段数据源

主界面调用字段界面时取两个数据源,
ds为记录数据源
dm为字段数据源

                SysData.DataHelper myHelper = new SysData.DataHelper();
                Hashtable ht = new Hashtable();
                ht.Add("@id", -1);
                DataSet ds = myHelper.GetDs(strSQL, ht);
                DataSet dm = myHelper.GetDs(strSQL, ht,true);
                BinSource.DataSource = ds.Tables[0];
                DBClass.BindControl(gBox, BinSource.DataSource,dm.Tables[0]);
                blnBind = true;
DBEdit自定义控件的字段绑定方式其中Mask.EditMask的设置尚未处理完成
            txtEdit.DataBindings.Clear();
            txtEdit.DataBindings.Add("EditValue", DataSource, DataField);

            DataTable dt = (DataTable)DataSchema;
            //判断字段类型,小数/整数/文本
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.Caption == DataField)
                {
                    //判断是否为空
                    Request = dc.AllowDBNull;
                    if (Request) label.ForeColor = Color.Black; else label.ForeColor = Color.Red;
                    //设置最大长度
                    txtEdit.Properties.MaxLength = dc.MaxLength;
                    //设置输入格式
                    if (dc.DataType == typeof(int) || dc.DataType == typeof(Int16) || dc.DataType == typeof(Int32) || dc.DataType == typeof(Int64))
                    {
                        txtEdit.Properties.Mask.EditMask = "";
                        txtEdit.Properties.Mask.MaskType = MaskType.Numeric;
                    }
                    else if (dc.DataType == typeof(string))
                    {
                        txtEdit.Properties.Mask.MaskType = MaskType.Simple;
                    }
                    //double类型会映射为decimal,待验证
                    else if (dc.DataType == typeof(Single) || dc.DataType == typeof(double) || dc.DataType == typeof(Decimal))
                    {
                        txtEdit.Properties.Mask.EditMask = "";
                        txtEdit.Properties.Mask.MaskType = MaskType.Numeric;
                    }
                    //其他类型暂不处理,待验证
                    //else MessageBox.Show("Other"+dc.DataType.Name);
                }
            }

DataHelper调用源码,此方式为Remoting与WebService的中间件调用
        /// 
        /// 带参数SQL,并返回结果数据集
        /// 
        /// 查询语句
        /// 参数
        /// 是否返回字段映射表
        /// 
        public DataSet GetDs(string strSQL, Hashtable parm, bool Schema=false)
        {
            string err = "";
            if (SysData.SysParam.intConnect == 0)
            {
                ChimService.Service myService = GetService();

                byte[] bytes = DataSetCompression.SerializeData(parm);

                byte[] bUserData = myService.GetOtherByte(ref err, SysData.SysParam.strDB, strSQL, bytes, Schema);
                if (err != "")
                {
                    MessageBox.Show(err, "错误");
                    err = "";
                    return null;
                }
                DataSet ds = DataSetCompression.DecompressionDataSet(bUserData);
                return ds;
            }
            else
            {
                CenterLib.Service obj = GetObj();
                byte[] bUserData = obj.GetOtherByte(ref err, SysData.SysParam.strDB, strSQL, parm);
                if (err != "")
                {
                    MessageBox.Show(err, "错误");
                    err = "";
                    return null;
                }
                DataSet ds = DataSetCompression.DecompressionDataSet(bUserData);
                return ds;
            }
        }

SQLHelper调用参考
       
 /// 
        /// 执行SQL命令 带命令类型 带参数 返回Dataset
        /// 
        /// SQL连接对象
        /// 命令类型
        /// SQL语句
        /// 参数
        /// 
        public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, bool Schema = false, params SqlParameter[] commandParameters)     //3
        {
            SqlCommand cmd = new SqlCommand();
            PrepareCommand(cmd, connection, null, commandType, commandText, commandParameters);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
	    da.Fill(ds);
if (Schema) da.FillSchema(ds, SchemaType.Mapped); cmd.Parameters.Clear(); return ds; }
 
     

你可能感兴趣的:(C#)