protected override void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control.GetType().Equals(typeof(DataGridViewTextBoxEditingControl)))//cell为类TextBox时 { e.CellStyle.BackColor = Color.FromName("window"); DataGridViewTextBoxEditingControl editingControl = e.Control as DataGridViewTextBoxEditingControl; editingControl.TextChanged -= new EventHandler(editingControl_TextChanged); editingControl.TextChanged += new EventHandler(editingControl_TextChanged); } else if (e.Control.GetType().Equals(typeof(DataGridViewLookupEditingControl))) { DataGridViewLookupEditingControl editingControl = e.Control as DataGridViewLookupEditingControl; string columnName = this.dataGridView1.CurrentCell.OwningColumn.Name; editingControl.valueSetted -= new LookupText.ValueSetted(productEditingControl_valueSetted); editingControl.valueSetted += new LookupText.ValueSetted(productEditingControl_valueSetted); if (columnName == "num") { ProductClothesInputNumForm form = editingControl.LookupForm as ProductClothesInputNumForm; LookupArg arg = this.dataGridView1.CurrentCell.OwningRow.Cells["product"].Value as LookupArg; form.setTitle(arg.Text); form.ProductId = (int)(arg.Value); editingControl.LookupArg = (this.dataGridView1.CurrentCell.OwningRow.Cells["num"] as DataGridViewLookupCell).Value as LookupArg; } } }
protected override void productEditingControl_valueSetted(object sender, LookupArg arg) { //File.AppendAllText("e:\\debug.txt", string.Format("value changed, thread:{0}\r\n", System.Threading.Thread.CurrentThread.ManagedThreadId)); DataGridViewLookupEditingControl control = (sender as DataGridViewLookupEditingControl); //File.AppendAllText("e:\\debug.txt", string.Format("value changed, control hash code={0}, control name={1}\r\n", control.GetHashCode(), control.Name)); try { //File.AppendAllText("e:\\debug.txt", string.Format("value changed, dataGridView hash code={0}, dataGridView name={1}\r\n", control.EditingControlDataGridView.GetHashCode(), control.EditingControlDataGridView.Name)); if (control.EditingControlDataGridView.Rows.Count == 0 || control.EditingControlDataGridView.CurrentCell == null) { throw new Exception(); } //if(control.EditingControlDataGridView.CurrentCell.OwningColumn.Name == "product") //这里为什么要判断呢? if (!string.IsNullOrEmpty(arg.ArgName) && arg.ArgName == "ProductStainless") { int productID = (int)(arg.Value); int oldID = -1; int.TryParse((control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["product"].Value).ToString(), out oldID); if (productID != oldID) { ProductStainless product = ProductStainlessDao.getInstance().FindByID(productID); //stone 临时关闭 control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["serial"].Value = product.Serial; control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["quantityPerPiece"].Value = product.QuantityPerPiece; control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["unit"].Value = product.Unit; if (conf.type == ProductCirculation.CirculationType.sell || conf.type == ProductCirculation.CirculationType.sellBack) { //这里要加个.ToString(),可能自定义Cell的value类型不大一样 control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["price"].Value = product.PriceSell.ToString(); } else { control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["price"].Value = product.PricePurchase.ToString(); } } } //not reasonal setSubTotalPrice(control.EditingControlRowIndex); setTotalPrice(); } catch (Exception ex) { //File.AppendAllText("e:\\debug.txt",string.Format("exception, dataGridView.Rows.Count={0}\r\n", this.dataGridView1.Rows.Count)); //File.AppendAllText("e:\\debug.txt", string.Format("exception, dataGridView hash code={0}, dataGridView name={1}\r\n", control.EditingControlDataGridView.GetHashCode(), control.EditingControlDataGridView.Name)); System.Threading.Thread.Sleep(0); } this.resetNeedSave(true); this.recordChanged = true; }
//for event: caculate total price protected override void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control.GetType().Equals(typeof(DataGridViewTextBoxEditingControl)))//cell为类TextBox时 { e.CellStyle.BackColor = Color.FromName("window"); DataGridViewTextBoxEditingControl editingControl = e.Control as DataGridViewTextBoxEditingControl; editingControl.TextChanged -= new EventHandler(editingControl_TextChanged); editingControl.TextChanged += new EventHandler(editingControl_TextChanged); } else if (e.Control.GetType().Equals(typeof(DataGridViewComboBoxEditEditingControl)))//cell为price时 { e.CellStyle.BackColor = Color.FromName("window"); DataGridViewComboBoxEditEditingControl editingControl = e.Control as DataGridViewComboBoxEditEditingControl; editingControl.Items.Clear(); int productID = 0, customerID = 0; DataGridViewLookupCell cell = editingControl.EditingControlDataGridView.Rows[editingControl.EditingControlRowIndex].Cells["product"] as DataGridViewLookupCell; if (cell.Value != null && !string.IsNullOrEmpty((cell.Value as LookupArg).ArgName)) { productID = (int)(cell.Value as LookupArg).Value; } if (this.lookupText1.LookupArg != null) { int.TryParse(this.lookupText1.LookupArg.Value.ToString(), out customerID); } if (productID != 0 && customerID != 0) { List <string> prices = ProductStainlessCirculationRecordDao.getInstance().FindPriceList((int)conf.type, productID, customerID); //items是放在column里 editingControl.Items.AddRange(prices.ToArray()); } editingControl.TextChanged -= new EventHandler(editingControl_TextChanged); editingControl.TextChanged += new EventHandler(editingControl_TextChanged); } else if (e.Control.GetType().Equals(typeof(DataGridViewLookupEditingControl))) { DataGridViewLookupEditingControl editingControl = e.Control as DataGridViewLookupEditingControl; string columnName = this.dataGridView1.CurrentCell.OwningColumn.Name; editingControl.valueSetted -= new LookupText.ValueSetted(productEditingControl_valueSetted); editingControl.valueSetted += new LookupText.ValueSetted(productEditingControl_valueSetted); } }
protected override void productEditingControl_valueSetted(object sender, LookupArg arg) { //File.AppendAllText("e:\\debug.txt", string.Format("value changed, thread:{0}\r\n", System.Threading.Thread.CurrentThread.ManagedThreadId)); DataGridViewLookupEditingControl control = (sender as DataGridViewLookupEditingControl); try { //File.AppendAllText("e:\\debug.txt", string.Format("value changed, dataGridView hash code={0}, dataGridView name={1}\r\n", control.EditingControlDataGridView.GetHashCode(), control.EditingControlDataGridView.Name)); if (control.EditingControlDataGridView.Rows.Count == 0 || control.EditingControlDataGridView.CurrentCell == null) { throw new Exception(); } //if(control.EditingControlDataGridView.CurrentCell.OwningColumn.Name == "product") if (!string.IsNullOrEmpty(arg.ArgName) && arg.ArgName == "Product") { int productID = (int)(arg.Value); int oldID = -1; int.TryParse((control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["product"].Value).ToString(), out oldID); if (productID != oldID) { control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["price"].Value = ProductClothesDao.getInstance().FindPriceByID(productID); DataGridViewLookupEditingControl lookup = sender as DataGridViewLookupEditingControl; this.setCellEnable(control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["num"], true); control.EditingControlDataGridView.Rows[control.EditingControlRowIndex].Cells["num"].Value = new LookupArg("", ""); } } //not reasonal setSubTotalPrice(control.EditingControlRowIndex); setTotalPrice(); } catch (Exception ex) { //File.AppendAllText("e:\\debug.txt",string.Format("exception, dataGridView.Rows.Count={0}\r\n", this.dataGridView1.Rows.Count)); //File.AppendAllText("e:\\debug.txt", string.Format("exception, dataGridView hash code={0}, dataGridView name={1}\r\n", control.EditingControlDataGridView.GetHashCode(), control.EditingControlDataGridView.Name)); System.Threading.Thread.Sleep(0); } this.resetNeedSave(true); this.recordChanged = true; }