private void RebindData(ViewItem vi) { if (vi.DataFrom.ToUpper().IndexOf("SELECT ") >= 0) { DataTable dtData = GetDataForm(vi); if (dtData != null) { switch (vi.ControlType) { case ViewControlType.Cbx: if (vi.ReleationInstance == null) { return; } (vi.ReleationInstance as ComboBox).DataSource = dtData; break; default: break; } } } }
/// <summary> /// 绑定数据显示 /// </summary> /// <param name="dtBind"></param> public void BindDataView(IDBQuery sysDb, string dataFrom, string dbSourceAlias) { string sqlParNames = SqlHelper.GetSqlPars(dataFrom); List <SqlParamInfo> sqlParValues = new List <SqlParamInfo>(); foreach (string curPar in sqlParNames.Split(',')) { object parValue = QueryPar(curPar); if (parValue != null) { if (parValue is DateTime) { sqlParValues.Add(new SqlParamInfo(curPar, DbType.DateTime, parValue)); } else if (parValue is int) { sqlParValues.Add(new SqlParamInfo(curPar, DbType.Int64, parValue)); } else if (parValue is double) { sqlParValues.Add(new SqlParamInfo(curPar, DbType.Double, parValue)); } else if (parValue is string) { sqlParValues.Add(new SqlParamInfo(curPar, DbType.String, parValue)); } else { sqlParValues.Add(new SqlParamInfo(curPar, DbType.String, parValue)); } } else { sqlParValues.Add(new SqlParamInfo(curPar, DbType.String, null)); } } DataTable dtBind = SqlHelper.GetDataSource(dataFrom, dbSourceAlias, sysDb, sqlParValues); foreach (Control ctl in lcDemo.Controls) { if (ctl.Tag == null) { continue; } ViewItem vi = ctl.Tag as ViewItem; string bindData = ""; if (dtBind != null && dtBind.Rows.Count > 0 && string.IsNullOrEmpty(vi.BindDataName) == false) { if (dtBind.Columns.Contains(vi.BindDataName)) { bindData = dtBind.Rows[0][vi.BindDataName].ToString(); } else { bindData = Convert.ToString(QueryPar(vi.BindDataName)); } } switch (vi.ControlType) { case ViewControlType.Lab: (ctl as Label).Text = bindData; break; case ViewControlType.Txt: (ctl as TextBox).Text = bindData; break; case ViewControlType.Cbx: (ctl as ComboBox).Text = bindData; break; case ViewControlType.Dtp: (ctl as DateTimePicker).Value = (bindData == "")?default(DateTime):Convert.ToDateTime(bindData); break; case ViewControlType.Checkbox: (ctl as CheckBox).Checked = (bindData == "") ? false : Convert.ToBoolean(bindData); break; default: break; } } if (lcDemo.Root != null) { if (_hideDataBind == null) { _hideDataBind = new Dictionary <string, object>(); } _hideDataBind.Clear(); string[] dbBindInfos = (lcDemo.Root.Text + ",").Split(','); foreach (string dbBindInfo in dbBindInfos) { if (string.IsNullOrEmpty(dbBindInfo)) { continue; } object parValue = QueryPar(dbBindInfo); if (parValue == null) { if (dtBind.Rows.Count > 0 && dtBind.Columns.Contains(dbBindInfo)) { parValue = dtBind.Rows[0][dbBindInfo].ToString(); } } _hideDataBind.Add(dbBindInfo, parValue); } } }
/// <summary> /// 载入布局 /// </summary> /// <param name="sqlFormatContext"></param> /// <param name="layoutContext"></param> public void LoadLayout(List <ViewItem> viewItems, string layoutFormat) { ClearLayout(); if (string.IsNullOrEmpty(layoutFormat)) { return; } _layoutControls = new List <ViewItem>(); lcDemo.BeginUpdate(); try { foreach (ViewItem vi in viewItems) { Control item = AddInputControl(vi); ViewItem viInstance = vi.Clone(); viInstance.ReleationInstance = item; _layoutControls.Add(viInstance); } using (MemoryStream ms = new MemoryStream()) { StreamWriter sw = new StreamWriter(ms); sw.Write(layoutFormat); sw.Flush(); ms.Position = 0; lcDemo.RestoreLayoutFromStream(ms); } foreach (ViewItem vi in _layoutControls) { if (vi.ReleationInstance == null) { continue; } LayoutControlItem lci = lcDemo.GetItemByControl(vi.ReleationInstance); if (lci == null) { continue; } vi.ReleationInstance.Font = lci.AppearanceItemCaption.Font; vi.ReleationInstance.ForeColor = lci.AppearanceItemCaption.ForeColor; vi.ReleationInstance.BackColor = lci.AppearanceItemCaption.BackColor; } } finally { lcDemo.EndUpdate(); } }
//private void comboBox_Enter(object sender, EventArgs e) //{ // try // { // ComboBox cbx = sender as ComboBox; // if (cbx == null) return; // ViewItem vi = cbx.Tag as ViewItem; // if (vi == null) return; // if (string.IsNullOrEmpty(vi.DataFrom)) return; // if (vi.DataFrom.ToUpper().IndexOf("SELECT ") >= 0) // { // string[] tmp = vi.DataFrom.Split('|'); // string dbAlias = ""; // string query = ""; // if (tmp.Length > 1) // { // dbAlias = tmp[0]; // query = tmp[1]; // } // else // { // query = tmp[0]; // } // string pars = SqlHelper.GetSqlPars(query); // if (string.IsNullOrEmpty(pars)) return; // bool hasPar = false; // foreach (ViewItem item in _layoutControls) // { // if (pars.IndexOf(item.InstanceName) >= 0) // { // hasPar = true; // break; // } // } // if (hasPar == false) return; // DataTable dtData = GetDataForm(vi); // if (dtData != null) // { // cbx.DataSource = dtData; // } // } // } // catch(Exception ex) // { // MsgBox.ShowException(ex, this); // } //} public Control AddInputControl(ViewItem vi) { LayoutControlItem lci = null; string ctlName = vi.InstanceName; //controlName; switch (vi.ControlType.ToUpper()) { case ViewControlType.Lab: Label lab = new Label(); lab.Name = ctlName; lab.Text = vi.DefaultValue; lab.Tag = vi; lci = lcDemo.Root.AddItem(ctlName, lab); lci.ShowInCustomizationForm = false; lci.Text = (vi.Caption == "") ? "标签" : vi.Caption; return(lab); case ViewControlType.Cbx: #region combobox处理 ComboBox cbx = new ComboBox(); cbx.Name = ctlName; cbx.Tag = vi; //从数据源加载数据 DataTable dtData = GetDataForm(vi); if (dtData != null) { if (dtData.Columns.Count > 1) { if (dtData.Columns.IndexOf("数据值") >= 0) { cbx.ValueMember = "数据值"; } else { cbx.ValueMember = dtData.Columns[0].ColumnName; } if (dtData.Columns.IndexOf("数据描述") >= 0) { cbx.DisplayMember = "数据描述"; } else { cbx.DisplayMember = dtData.Columns[1].ColumnName; } } else { cbx.DisplayMember = dtData.Columns[0].ColumnName; cbx.ValueMember = dtData.Columns[0].ColumnName; } cbx.DataSource = dtData; } lci = lcDemo.Root.AddItem(ctlName, cbx); lci.ShowInCustomizationForm = false; lci.Text = (vi.Caption == "") ? "下拉框" : vi.Caption; if (string.IsNullOrEmpty(vi.DefaultValue) == false) { //设置默认值 int selIndex = cbx.Items.IndexOf(vi.DefaultValue); if (selIndex < 0) { selIndex = cbx.FindString(vi.DefaultValue); } if (selIndex >= 0) { cbx.SelectedIndex = selIndex; } else { cbx.Text = vi.DefaultValue; } } else { cbx.SelectedIndex = cbx.Items.Count - 1; } if (vi.ReadOnly) { cbx.DropDownStyle = ComboBoxStyle.DropDownList; } cbx.TextChanged += ElementTextChange; cbx.SelectedIndexChanged += ElementTextChange; //cbx.Enter += comboBox_Enter; //cbx.KeyPress += OnKeyPress; return(cbx); #endregion case ViewControlType.Txt: #region textbox处理 TextBox tb = new TextBox(); tb.Name = ctlName; tb.Tag = vi; tb.Text = vi.DefaultValue; //设置默认值 lci = lcDemo.Root.AddItem(ctlName, tb); lci.ShowInCustomizationForm = false; lci.Text = (vi.Caption == "") ? "文本框" : vi.Caption; tb.Enabled = !vi.ReadOnly; tb.TextChanged += ElementTextChange; //tb.KeyPress += OnKeyPress; return(tb); #endregion case ViewControlType.Dtp: #region datetimepicker处理 DateTimePicker dtp = new DateTimePicker(); dtp.Name = ctlName; dtp.Tag = vi; try { if (string.IsNullOrEmpty(vi.DefaultValue) == false) { //设置默认值 dtp.Value = Convert.ToDateTime(vi.DefaultValue); } } catch { } lci = lcDemo.Root.AddItem(ctlName, dtp); lci.ShowInCustomizationForm = false; lci.Text = (vi.Caption == "") ? "日期框" : vi.Caption; dtp.Enabled = !vi.ReadOnly; dtp.ValueChanged += ElementTextChange; //dtp.KeyPress += OnKeyPress; return(dtp); #endregion case ViewControlType.Checkbox: #region checkbox处理 CheckBox cb = new CheckBox(); cb.Name = ctlName; cb.Tag = vi; cb.Text = vi.Caption; try { if (string.IsNullOrEmpty(vi.DefaultValue) == false) { //设置默认值 cb.Checked = Convert.ToBoolean(vi.DefaultValue); } } catch { } lci = lcDemo.Root.AddItem(ctlName, cb); lci.ShowInCustomizationForm = false; lci.Text = (vi.Caption == "") ? "复选框" : vi.Caption; cb.Enabled = !vi.ReadOnly; cb.CheckedChanged += ElementTextChange; //cb.KeyPress += OnKeyPress; return(cb); #endregion default: return(null); } }
private DataTable GetDataForm(ViewItem vi) { if (string.IsNullOrEmpty(vi.DataFrom)) { return(null); } string datafrom = vi.DataFrom; //判断是否查询语句 if (datafrom.ToUpper().IndexOf("SELECT ") >= 0) { string[] tmp = vi.DataFrom.Split('|'); string dbAlias = ""; string query = ""; if (tmp.Length > 1) { dbAlias = tmp[0]; query = tmp[1]; } else { query = tmp[0]; } //查询语句处理 if (DBHelper != null) { DataTable dtBind = SqlHelper.GetDataSource(query, dbAlias, DBHelper, QueryPar); DataRow drNull = dtBind.NewRow(); if (dtBind.PrimaryKey.Length > 0) { foreach (DataColumn dc in dtBind.PrimaryKey) { if (dc.DataType == typeof(string)) { drNull[dc] = "0"; } else { drNull[dc] = 0; } } } dtBind.Rows.Add(drNull); return(dtBind); } else { MessageBox.Show("尚未初始化数据查询对象。", "提示"); return(null); } } else { //字符串处理 string[] aryDatas = (datafrom + ";").Split(';'); DataTable dtData = new DataTable(); dtData.Columns.Add("数据值", typeof(string)); dtData.Columns.Add("数据描述", typeof(string)); foreach (string dataItem in aryDatas) { if (string.IsNullOrEmpty(dataItem)) { continue; } DataRow dr = dtData.NewRow(); string[] parseData = (dataItem + "-" + dataItem).Split('-'); dr["数据值"] = parseData[0]; dr["数据描述"] = parseData[1]; dtData.Rows.Add(dr); } DataRow drNull = dtData.NewRow(); dtData.Rows.Add(drNull); return(dtData); } }