public void Execute(bool progress) { if ((SrcDataSorce != null || SrcDataSorce != "") && (DestDataSorce != "" || DestDataSorce != null)) { bool tempSrc = false; bool tempDest = false; WebGridView wgv = new WebGridView(); WebDetailsView wdv = new WebDetailsView(); WebFormView wfv = new WebFormView(); WebDataSource wsSrc = new WebDataSource(); DataSet dsSrc = new DataSet(); WebDataSource wsDest = new WebDataSource(); DataSet dsDest = new DataSet(); WebDataSource wsDestM = new WebDataSource(); foreach (System.Web.UI.Control c in this.Page.Form.Controls) { if (c is WebDataSource && (c as WebDataSource).ID == this.SrcDataSorce) { wsSrc = c as WebDataSource; tempSrc = wsSrc.AutoApply; wsSrc.AutoApply = false; dsSrc = wsSrc.InnerDataSet; } if (c is WebDataSource && (c as WebDataSource).ID == this.DestDataSorce) { wsDest = c as WebDataSource; tempDest = wsDest.AutoApply; wsDest.AutoApply = false; dsDest = wsDest.InnerDataSet; foreach (System.Web.UI.Control ctrl in this.Page.Form.Controls) if (ctrl is WebDataSource && (ctrl as WebDataSource).ID == wsDest.MasterDataSource) { wsDestM = ctrl as WebDataSource; //added by lily 2006/12/14 for detail has no innerdataset dsDest = wsDestM.InnerDataSet; //added by lily 2006/12/14 for detail has no innerdataset break; } } } bool isGet = false; DataTable dtSrc = new DataTable(); for (int i = 0; i < dsSrc.Relations.Count; i++) foreach (System.Web.UI.Control c in this.Page.Form.Controls) if (c is WebDataSource && (c as WebDataSource).ID == this.SrcDataSorce && (c as WebDataSource).DataMember == dsSrc.Relations[i].ChildTable.ToString()) { dtSrc = dsSrc.Relations[i].ChildTable; isGet = true; } if (isGet == false) if (dsSrc.Tables.Count > 0) dtSrc = dsSrc.Tables[0]; else dtSrc = wsSrc.CommandTable; DataTable dtDest = new DataTable(); isGet = false; for (int i = 0; i < dsDest.Relations.Count; i++) foreach (System.Web.UI.Control c in this.Page.Form.Controls) if (c is WebDataSource && (c as WebDataSource).ID == this.DestDataSorce && (c as WebDataSource).DataMember == dsDest.Relations[i].ChildTable.ToString()) { dtDest = dsDest.Relations[i].ChildTable; isGet = true; } if (isGet == false) dtDest = dsDest.Tables[0]; DataTable dtDestM = new DataTable(); isGet = false; for (int i = 0; i < wsDestM.InnerDataSet.Relations.Count; i++) if (this.DestDataSorce == wsDestM.InnerDataSet.Relations[i].ChildTable.ToString()) { dtDestM = wsDestM.InnerDataSet.Relations[i].ChildTable; isGet = true; } if (isGet == false) dtDestM = wsDestM.InnerDataSet.Tables[0]; DataTable dsDestMaster =dsDest.Tables[0]; string MasterKey = null; foreach (System.Web.UI.Control c in this.Page.Form.Controls) { if (c is WebFormView) foreach (System.Web.UI.Control ctrl in this.Page.Form.Controls) if (ctrl is WebDataSource && (ctrl as WebDataSource).ID == (c as WebFormView).DataSourceID && (ctrl as WebDataSource).DataMember == dsDestMaster.ToString()) { MasterKey = dsDestMaster.Rows[(c as WebFormView).DataItemIndex][dsDestMaster.PrimaryKey[0].ToString()].ToString(); break; } if (c is WebDetailsView) foreach (System.Web.UI.Control ctrl in this.Page.Form.Controls) if (ctrl is WebDataSource && (ctrl as WebDataSource).ID == (c as WebDetailsView).DataSourceID && (ctrl as WebDataSource).DataMember == dsDestMaster.ToString()) { MasterKey = dsDestMaster.Rows[(c as WebDetailsView).DataItemIndex][dsDestMaster.PrimaryKey[0].ToString()].ToString(); break; } } //modified by lily 2006/12/14 for: List<> can be added when needed, DataRow[] cannot //DataRow[] drDestTemp = new DataRow[dtDest.Rows.Count]; List<DataRow> drDestTemp = new List<DataRow>(); for (int i = 0; i < dtDest.Rows.Count; i++) if (dtDest.Rows[i][dsDestMaster.PrimaryKey[0].ToString()].ToString() == MasterKey) drDestTemp.Add(dtDest.Rows[i]); //drDestTemp[i] = dtDest.Rows[i]; ArrayList strSrc = new ArrayList(); ArrayList strDest = new ArrayList(); foreach (WebKeyMatchColumn kmc in this.KeyMatchColumns) if (kmc.SrcColumnName == null || kmc.DestColumnName == null || kmc.SrcColumnName == "" || kmc.DestColumnName == "") { language = CliUtils.fClientLang; string message = SysMsg.GetSystemMessage(language, "Srvtools", "InfoClientMove", "iskeyMatchColumn", true); this.Page.Response.Write("<script>alert(\"" + message + "\");</script>"); return; } else { strSrc.Add(kmc.SrcColumnName); strDest.Add(kmc.DestColumnName); } for (int i = 0; i < dtSrc.Rows.Count; i++) { int flag = 0; Hashtable ht = new Hashtable(); if (this.AlwaysInsert == false && strSrc != null && drDestTemp.Count > 0) for (int j = 0; j < drDestTemp.Count; j++) { for (int x = 0; x < strSrc.Count; x++) if (drDestTemp[j] != null && dtSrc.Rows[i][strSrc[x].ToString()].ToString() == drDestTemp[j][strDest[x].ToString()].ToString()) flag++; if (flag == strSrc.Count) { if (this.AlwaysReplace == true) { for (int x = 0; x < this.MatchColumns.Count; x++) { WebMatchColumn mc = this.MatchColumns[x]; if (mc.Expression != null && mc.Expression != "") { DataColumn dcExpression = new DataColumn(); dcExpression.ColumnName = mc.Expression; dcExpression.Expression = mc.Expression; if (dtSrc.Columns.Contains(dcExpression.ColumnName) == false) dtSrc.Columns.Add(dcExpression); drDestTemp[j][mc.DestColumnName] = dtSrc.Rows[i][mc.Expression].ToString(); } else drDestTemp[j][mc.DestColumnName] = dtSrc.Rows[i][mc.SrcColumnName].ToString(); } OnAfterMove(new EventArgs()); } break; } else flag = 0; } if (flag != strSrc.Count || strSrc.Count == 0) { DataRow dr = dtDest.NewRow(); ht.Add(dsDest.Tables[0].PrimaryKey[0].ToString(), MasterKey); foreach (WebMatchColumn mc in this.MatchColumns) if (mc.Expression != null && mc.Expression != "") { DataColumn dcExpression = new DataColumn(); dcExpression.ColumnName = mc.Expression; dcExpression.Expression = mc.Expression; if (dtSrc.Columns.Contains(dcExpression.ColumnName) == false) dtSrc.Columns.Add(dcExpression); ht.Add(mc.DestColumnName, dtSrc.Rows[i][mc.Expression]); } else { if ((mc.SrcColumnName == "" || mc.DestColumnName == "" || mc.SrcColumnName == null || mc.DestColumnName == null) && (mc.Expression == null || mc.Expression == "")) { language = CliUtils.fClientLang; string message = SysMsg.GetSystemMessage(language, "Srvtools", "InfoClientMove", "ismatchColumn", true); this.Page.Response.Write("<script>alert(\"" + message + "\");</script>"); return; } else ht.Add(mc.DestColumnName, dtSrc.Rows[i][mc.SrcColumnName]); } OnAfterMove(new EventArgs()); int isExsite = 0; for (int j = 0; j < dtDestM.Rows.Count; j++) { isExsite = 0; for (int x = 0; x < dtDestM.Columns.Count; x++) if (ht[dtDestM.Columns[x].ToString()] != null && ht[dtDestM.Columns[x].ToString()].ToString() == dtDestM.Rows[j][dtDestM.Columns[x].ToString()].ToString()) isExsite++; if (isExsite == ht.Count) break; } if (isExsite != ht.Count) { wsDest.Insert(ht); OnAfterInsert(new EventArgs()); } } } wsSrc.AutoApply = tempSrc; wsDest.AutoApply = tempDest; if (progress == true) { System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(LongTask)); thread.Start(); this.Page.Session["State"] = 1; OpenProgressBar(this.Page); } this.Page.DataBind(); } }
private void doOkForGridInsert(WebGridView gridView, WebDataSource ds) { Hashtable ht = new Hashtable(); for (int i = 0; i < gridView.Columns.Count; i++) { string fieldName; if (gridView.Columns[i] is BoundField) { fieldName = ((BoundField)gridView.Columns[i]).DataField; if (!((BoundField)gridView.Columns[i]).ReadOnly) { TextBox txt = (TextBox)gridView.FooterRow.FindControl("InfoTextBox" + fieldName); if (txt != null) { if (txt.Text == "") { ht.Add(fieldName, DBNull.Value); } else { ht.Add(fieldName, txt.Text); } } } else { Label lbl = (Label)gridView.FooterRow.FindControl("InfoLabel" + fieldName); if (lbl != null) { if (lbl.Text == "") { ht.Add(fieldName, DBNull.Value); } else { ht.Add(fieldName, lbl.Text); } } } } else if (gridView.Columns[i] is TemplateField) { fieldName = gridView.Columns[i].SortExpression; object strValue = GetValue(fieldName, gridView); if (strValue == null || strValue.ToString() == "") //modified by lily 2007/4/12 如果多个Templatefield,且没有设定SortExpression,会报错。 { if (fieldName != null && fieldName != "") ht.Add(fieldName, DBNull.Value); } else ht.Add(fieldName, strValue); } } WebValidate validate = (WebValidate)gridView.ExtendedFindChildControl(gridView.DataSourceID, FindControlType.DataSourceID, typeof(WebValidate)); if (validate != null) { validate.Text = string.Empty; if (ds.PrimaryKey.Length > 0) { object[] value = new object[ds.PrimaryKey.Length]; for (int i = 0; i < ds.PrimaryKey.Length; i++) { string columnName = ds.PrimaryKey[i].ColumnName; if (ht.ContainsKey(columnName)) { value[i] = ht[columnName]; } else if (ds.RelationValues != null && ds.RelationValues.Contains(columnName)) { value[i] = ds.RelationValues[columnName]; } else { throw new EEPException(EEPException.ExceptionType.ColumnValueNotFound, ds.GetType(), ds.ID, columnName, null); } } if (!validate.CheckDuplicate(ds, value)) { gridView.ValidateFailed = true; return; } } bool validateSucess = validate.CheckValidate(ht); if (validateSucess) { gridView.ValidateFailed = false; ds.Insert(ht); gridView.GridInserting = false; gridView.OnAfterInsert(EventArgs.Empty); if (!gridView.TotalActive) gridView.ShowFooter = false; else gridView.DataBind(); } else { gridView.ValidateFailed = true; gridView.ValidateFormat(); } if (gridView.ValidateFailed) return; } else { ds.Insert(ht); gridView.GridInserting = false; gridView.OnAfterInsert(EventArgs.Empty); if (!gridView.TotalActive) gridView.ShowFooter = false; else gridView.DataBind(); } WebNavigator bindingNav = gridView.GetBindingNavigator(); if (bindingNav != null && bindingNav != this) { if (ds.AutoApply) { bindingNav.SetState(WebNavigator.NavigatorState.ApplySucess); bindingNav.SetNavState("Browsed"); } else { bindingNav.SetState(WebNavigator.NavigatorState.Changed); bindingNav.SetNavState("Changing"); } } if (ds.AutoApply) { this.SetState(WebNavigator.NavigatorState.ApplySucess); this.SetNavState("Browsed"); } else { this.SetState(WebNavigator.NavigatorState.Changed); this.SetNavState("Changing"); } }