You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

772 lines
31 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using Mesnac.Action.Base;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;
using Mesnac.Action.ChemicalWeighing.Entity;
using Mesnac.Action.ChemicalWeighing.Technical;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe;
using System.Configuration;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe.entity;
using System.Linq;
namespace Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe
{
public partial class FrmRecipe : Form
{
#region 字段定义
private ActionType _actionType = ActionType.Add; //操作类型0-为添加1-为修改
private string _recipeName = null;
private int _mixerNum = 0;
private int _groutBags = 0;
private string _remark = null;
private List<Base_MaterialInfo> pmt_Materials = new List<Base_MaterialInfo>(); //所有物料集合
private decimal _totalWeight = 0M;
private decimal _totalError = 0M;
private string _curRecipeID = null;
private List<Pmt_weigh> pmt_Weighs = null; //待写入数据库的配方物料信息List
private List<Base_RecipeMaterial> base_RecipeMaterials = null;
private Base_RecipeInfo modifyRecipeInfo = null;
#endregion
#region 属性定义
public string RecipeName { get => _recipeName; set => _recipeName = value; }
public int MixerNum { get => _mixerNum; set => _mixerNum = value; }
public int GroutBags { get => _groutBags; set => _groutBags = value; }
public string Remark { get => _remark; set => _remark = value; }
public List<Base_MaterialInfo> Pmt_Materials { get => pmt_Materials; set => pmt_Materials = value; }
public decimal TotalWeight { get => _totalWeight; set => _totalWeight = value; }
public decimal TotalError { get => _totalError; set => _totalError = value; }
public string CurRecipeID { get => _curRecipeID; set => _curRecipeID = value; }
#endregion
#region 构造方法
public FrmRecipe()
{
InitializeComponent();
}
/// <summary>
/// 构造方法
/// </summary>
/// <param name="actionType">操作类型0-为添加1-为修改</param>
public FrmRecipe(ActionType actionType)
{
InitializeComponent();
this._actionType = actionType;
base_RecipeMaterials = new List<Base_RecipeMaterial>();
}
/// <summary>
/// 修改构造方法
/// </summary>
/// <param name="actionType">操作类型0-为添加1-为修改</param>
public FrmRecipe(ActionType actionType,Base_RecipeInfo recipeInfo)
{
InitializeComponent();
this._actionType = actionType;
modifyRecipeInfo = recipeInfo;
base_RecipeMaterials = new List<Base_RecipeMaterial>();
}
#endregion
#region 方法定义
/// <summary>
/// 物料名称集合获取
/// </summary>
public void InitCombox()
{
}
public void InitRecipeTypeInfo()
{
//获取物料信息表
DataTable RecipeTypeTypeTable = TechnicalHelper.getRecipeType();
cb_RecipeType.DataSource = RecipeTypeTypeTable;
cb_RecipeType.DisplayMember = "recipetype_Name";
cb_RecipeType.ValueMember = "recipetype_Name";
}
/// <summary>
/// 初始化界面文本
/// </summary>
public void InitUI()
{
if (this._actionType == ActionType.Add)
{
this.Text = "添加新配方";
txtRecipeName.Enabled = true;
textBox9.Enabled = false;
textBox9.ReadOnly = true;
//加载配方类别下拉框
InitRecipeTypeInfo();
}
else if (this._actionType == ActionType.Modify)
{
InitRecipeTypeInfo();
this.Text = "修改配方";
txtRecipeName.Text = modifyRecipeInfo.recipeName;
txtRecipeName.Enabled = false;
txtRecipeId.Text = modifyRecipeInfo.recipeId;
txtRecipeId.Enabled = false;
cb_RecipeType.Text = modifyRecipeInfo.recipeType;
textBox9.Text = modifyRecipeInfo.recipeWeight.ToString();
textBox9.Enabled = false;
txtRemark.Text = modifyRecipeInfo.remark;
}
else if (this._actionType == ActionType.SaveAs)
{
this.Text = "另存配方";
//txtRecipeName.Text = modifyRecipeInfo.recipeName;
//txtRecipeId.Text = modifyRecipeInfo.recipeId;
cb_RecipeType.Text = modifyRecipeInfo.recipeType;
textBox9.Text = modifyRecipeInfo.recipeWeight.ToString();
textBox9.Enabled = false;
txtRemark.Text = modifyRecipeInfo.remark;
}
this.btnOk.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnOK"));
this.btnCancel.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnCancel"));
}
/// <summary>
/// 初始化配方信息
/// </summary>
public void InitData()
{
if (this._actionType == ActionType.Add)
{
//this.DGVMaterialSet.Rows.Add();
}
if (this._actionType == ActionType.Modify || this._actionType == ActionType.SaveAs)
{
//根据配方编号获取关联物料信息
DataTable dataTable = RecipeHelper.GetRecipeMaterialInfo(modifyRecipeInfo.recipeId);
////先添加好空行
if (dataTable != null && dataTable.Rows.Count > 0)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
//this.DGVMaterialSet.Rows.Add();
//再将数据填入
if (i == 0)
{
tb_A_Weight.Text = dataTable.Rows[i][4].ToString();
}
else if (i == 1)
{
tb_B_Weight.Text = dataTable.Rows[i][4].ToString();
}
else if (i == 2)
{
tb_C_Weight.Text = dataTable.Rows[i][4].ToString();
}
else if (i == 3)
{
tb_Second_A_Weight.Text = dataTable.Rows[i][4].ToString();
}
else if (i == 4)
{
tb_Second_B_Weight.Text = dataTable.Rows[i][4].ToString();
}
}
}
//根据配方编号填充运行参数
List<Base_RecipeCratParam> recipeCratParams = Technical.PmtRecipe.RecipeHelper.GerCratParamListByRecipeAndMaterial(modifyRecipeInfo.recipeId, string.Empty);
Base_RecipeCratParam base_RecipeCratParam1 = recipeCratParams.Where(x => x.paramName.Contains("混料速度1")).FirstOrDefault();
textBox1.Text = base_RecipeCratParam1.paramValue;
Base_RecipeCratParam base_RecipeCratParam2 = recipeCratParams.Where(x => x.paramName.Contains("混料速度2")).FirstOrDefault();
textBox2.Text = base_RecipeCratParam2.paramValue;
Base_RecipeCratParam base_RecipeCratParam3 = recipeCratParams.Where(x => x.paramName.Contains("混料速度3")).FirstOrDefault();
textBox3.Text = base_RecipeCratParam3.paramValue;
Base_RecipeCratParam base_RecipeCratParam4 = recipeCratParams.Where(x => x.paramName.Contains("进料时间")).FirstOrDefault();
textBox4.Text = base_RecipeCratParam4.paramValue;
Base_RecipeCratParam base_RecipeCratParam5 = recipeCratParams.Where(x => x.paramName.Contains("混料时间")).FirstOrDefault();
textBox5.Text = base_RecipeCratParam5.paramValue;
Base_RecipeCratParam base_RecipeCratParam6 = recipeCratParams.Where(x => x.paramName.Contains("罐A加料误差")).FirstOrDefault();
textBox6.Text = base_RecipeCratParam6.paramValue;
Base_RecipeCratParam base_RecipeCratParam7 = recipeCratParams.Where(x => x.paramName.Contains("罐B加料误差")).FirstOrDefault();
textBox7.Text = base_RecipeCratParam7.paramValue;
Base_RecipeCratParam base_RecipeCratParam8 = recipeCratParams.Where(x => x.paramName.Contains("树脂加料误差")).FirstOrDefault();
textBox8.Text = base_RecipeCratParam8.paramValue;
}
}
/// <summary>
/// 填充物料信息List
/// </summary>
public void GetPmtWeightList()
{
//base_RecipeMaterials.Clear();
//for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
//{
// Base_RecipeMaterial baseRepiceMaterial = new Base_RecipeMaterial();
// baseRepiceMaterial.recipeId = txtRecipeId.Text;
// for (int j = 0; j < DGVMaterialSet.ColumnCount; j++)
// {
// if (j == 0)
// {
// //根据物料名称获取物料ID
// string materialNameStr = DGVMaterialSet.Rows[i].Cells[j].Value.ToString();
// baseRepiceMaterial.materialName = materialNameStr;
// if (pmt_Materials.Exists(x => x.materialName == materialNameStr))
// {
// baseRepiceMaterial.materialId = GetMaterialID(materialNameStr);
// }
// }
// else if (j == 1)
// {
// baseRepiceMaterial.materialWeight = Convert.ToDecimal(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
// }
// else if (j == 2)
// {
// baseRepiceMaterial.putTime = Convert.ToInt16(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
// }
// }
// base_RecipeMaterials.Add(baseRepiceMaterial);
//}
}
/// <summary>
/// 根据物料名称查找物料ID
/// </summary>
public string GetMaterialID(string materialName)
{
Base_MaterialInfo findPmt_Material = pmt_Materials.FindLast(x => x.materialName == materialName);
return findPmt_Material.materialId;
}
/// <summary>
/// 根据物料ID查找物料名称
/// </summary>
public string GetMaterialName(string materialID)
{
Base_MaterialInfo findPmt_Material = pmt_Materials.FindLast(x => x.materialId == materialID);
return findPmt_Material.materialName;
}
/// <summary>
/// 计算TotalWeight和TotalError
/// </summary>
public void GetTotalWeightErrorValue()
{
//for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
//{
// for (int j = 1; j < DGVMaterialSet.ColumnCount; j++)
// {
// if (j == 1)
// {
// double tempd = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
// _totalWeight = _totalWeight + (decimal)tempd;
// }
// if (j == 2)
// {
// double tempe = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
// _totalError = _totalError + (decimal)tempe;
// }
// }
//}
}
/// <summary>
/// 计算配方总重
/// </summary>
public void GetTotalWeightValue()
{
}
/// <summary>
/// 向数据库中插入新配方数据
/// </summary>
public void NewRecipeAdd()
{
Base_RecipeInfo base_RepiceInfo = new Base_RecipeInfo()
{
recipeName = txtRecipeName.Text,
recipeId = txtRecipeId.Text,
recipeType = cb_RecipeType.Text,
recipeWeight = Convert.ToDecimal(tb_A_Weight.Text) + Convert.ToDecimal(tb_B_Weight.Text) + Convert.ToDecimal(tb_C_Weight.Text)+ Convert.ToDecimal(tb_Second_A_Weight.Text)+ Convert.ToDecimal(tb_Second_B_Weight.Text),
recipeState = 0,
editUser = "admin",
editTime = DateTime.Now,
remark = txtRemark.Text
};
if (this._actionType == ActionType.Add || this._actionType == ActionType.Modify)
{
Base_RecipeCratParam cratParam1 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label8.Text,
paramValue = textBox1.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam2 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label9.Text,
paramValue = textBox2.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam3 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label10.Text,
paramValue = textBox3.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam4 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label11.Text,
paramValue = textBox4.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam5 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label13.Text,
paramValue = textBox5.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam6 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label19.Text,
paramValue = textBox6.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam7 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label26.Text,
paramValue = textBox7.Text,
editUser = "admin",
};
Base_RecipeCratParam cratParam8 = new Base_RecipeCratParam()
{
recipeId = txtRecipeId.Text,
paramName = label28.Text,
paramValue = textBox8.Text,
editUser = "admin",
};
RecipeHelper.InsertCratParam(cratParam1);
RecipeHelper.InsertCratParam(cratParam2);
RecipeHelper.InsertCratParam(cratParam3);
RecipeHelper.InsertCratParam(cratParam4);
RecipeHelper.InsertCratParam(cratParam5);
RecipeHelper.InsertCratParam(cratParam6);
RecipeHelper.InsertCratParam(cratParam7);
RecipeHelper.InsertCratParam(cratParam8);
}
_curRecipeID = RecipeHelper.InsertRecipe(base_RepiceInfo);
}
/// <summary>
/// 配方物料信息插入数据库
/// </summary>
public void NewWeighAdd()
{
DateTime dateTime = DateTime.Now;
try
{
List<Base_MaterialInfo> Material_A = MaterialManage.MaterialHelper.GetBaseMaterilaInfo(1);
List<Base_MaterialInfo> Material_B = MaterialManage.MaterialHelper.GetBaseMaterilaInfo(2);
List<Base_MaterialInfo> Material_C = MaterialManage.MaterialHelper.GetBaseMaterilaInfo(3);
Base_RecipeMaterial tempPmt_Weigh1 = new Base_RecipeMaterial()
{
recipeId = txtRecipeId.Text,
materialId = Material_A[0].materialId,
materialName = Material_A[0].materialName,
materialWeight = Convert.ToDecimal(tb_A_Weight.Text),
putTime = 1,
editUser = "admin",
editTime = dateTime
};
Base_RecipeMaterial tempPmt_Weigh2 = new Base_RecipeMaterial()
{
recipeId = txtRecipeId.Text,
materialId = Material_B[0].materialId,
materialName = Material_B[0].materialName,
materialWeight = Convert.ToDecimal(tb_B_Weight.Text),
putTime = 1,
editUser = "admin",
editTime = dateTime
};
Base_RecipeMaterial tempPmt_Weigh3 = new Base_RecipeMaterial()
{
recipeId = txtRecipeId.Text,
materialId = Material_C[0].materialId,
materialName = Material_C[0].materialName,
materialWeight = Convert.ToDecimal(tb_C_Weight.Text),
putTime = 1,
editUser = "admin",
editTime = dateTime
};
Base_RecipeMaterial tempPmt_Weigh4 = new Base_RecipeMaterial()
{
recipeId = txtRecipeId.Text,
materialId = "4",
materialName = Material_A[0].materialName + "二次应配",
materialWeight = Convert.ToDecimal(tb_Second_A_Weight.Text),
putTime = 1,
editUser = "admin",
editTime = dateTime
};
Base_RecipeMaterial tempPmt_Weigh5 = new Base_RecipeMaterial()
{
recipeId = txtRecipeId.Text,
materialId = "5",
materialName = Material_B[0].materialName + "二次应配",
materialWeight = Convert.ToDecimal(tb_Second_B_Weight.Text),
putTime = 1,
editUser = "admin",
editTime = dateTime
};
RecipeHelper.InsertWeigh(tempPmt_Weigh1);
RecipeHelper.InsertWeigh(tempPmt_Weigh2);
RecipeHelper.InsertWeigh(tempPmt_Weigh3);
RecipeHelper.InsertWeigh(tempPmt_Weigh4);
RecipeHelper.InsertWeigh(tempPmt_Weigh5);
}
catch (Exception ex)
{
throw;
}
//if(base_RecipeMaterials != null && base_RecipeMaterials.Count > 0)
//{
// foreach(Base_RecipeMaterial tempPmt_Weigh in base_RecipeMaterials)
// {
// tempPmt_Weigh.editUser = "admin";
// RecipeHelper.InsertWeigh(tempPmt_Weigh);
// }
//}
}
/// <summary>
/// 配方物料信息删除
/// </summary>
public void WeighDelByRecipeID(string recipeID)
{
RecipeHelper.DelWeighByRecipeID(recipeID);
}
/// <summary>
/// 物料名称重复检查
/// </summary>
/// <returns></returns>
private bool DGVMaterialRepeatCheck()
{
bool checkResult = false;
//if (DGVMaterialSet.Rows.Count == 1)
//{
// checkResult = false;
//}
//else
//{
// for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
// {
// if (DGVMaterialSet.Rows[i].Cells[0].Value == null)
// {
// return true;
// }
// string material1Name = DGVMaterialSet.Rows[i].Cells[0].Value.ToString();
// for (int j = i + 1; j < DGVMaterialSet.Rows.Count; j++)
// {
// if (DGVMaterialSet.Rows[j].Cells[0].Value == null)
// {
// return true;
// }
// if (DGVMaterialSet.Rows[j].Cells[0].Value.ToString() == material1Name)
// {
// checkResult = true;
// break;
// }
// }
// if (checkResult)
// {
// break;
// }
// }
//}
return checkResult;
}
#endregion
#region 事件处理
private void FrmPlan_Load(object sender, EventArgs e)
{
this.InitCombox();
this.InitUI();
this.InitData();
}
private void FrmPlan_Activated(object sender, EventArgs e)
{
if (this._actionType == ActionType.Modify)
{
this.txtRecipeId.Focus();
}
else
{
this.txtRecipeName.Focus();
}
}
private void btnOk_Click(object sender, EventArgs e)
{
#region 信息验证
if (String.IsNullOrEmpty(this.txtRecipeName.Text))
{
MessageBox.Show("请输入新配方的名称!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.txtRecipeId.Text))
{
MessageBox.Show("请输入新配方的编号!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.cb_RecipeType.Text))
{
MessageBox.Show("请输入新配方的类别!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//if (DGVMaterialSet == null)
//{
// MessageBox.Show("请设置配方的物料信息!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
//if (DGVMaterialRepeatCheck())
//{
// MessageBox.Show("物料列表中存在重复物料或者空物料,请修改.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
if (String.IsNullOrEmpty(this.tb_A_Weight.Text))
{
MessageBox.Show("请输入新配方的罐A应配重量.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.tb_B_Weight.Text))
{
MessageBox.Show("请输入新配方的罐B应配重量.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.tb_C_Weight.Text))
{
MessageBox.Show("请输入新配方的树脂应配重量.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.tb_Second_A_Weight.Text))
{
MessageBox.Show("罐A二次应配重量不允许为空,请修改.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.tb_Second_B_Weight.Text))
{
MessageBox.Show("罐B二次应配重量不允许为空,请修改.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//if (DGVMaterialSet != null && DGVMaterialSet.Rows.Count > 0)
//{
// bool errorFlag = false;
// for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
// {
// for (int j = 0; j < DGVMaterialSet.ColumnCount; j++)
// {
// if (DGVMaterialSet.Rows[i].Cells[j].Value == null)
// {
// errorFlag = true;
// break;
// }
// }
// if (errorFlag)
// {
// break;
// }
// }
// if (errorFlag)
// {
// MessageBox.Show("请将物料信息填写完整!或删除无用物料!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
// }
//}
if (_actionType == ActionType.Add && RecipeHelper.IsExistsName(this.txtRecipeName.Text))
{
MessageBox.Show("新配方的名称重复!请使用其他名称", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 计算TotalWeight和TotalError
//GetTotalWeightErrorValue();
GetTotalWeightValue();
#endregion
#region 填充物料信息List
//GetPmtWeightList();
#endregion
#region 配方及物料信息写入数据库
if(_actionType == ActionType.Add)
{
this.NewRecipeAdd(); //新配方数据插入数据库
this.NewWeighAdd(); //配方对应的物料信息插入数据库
}
else if (_actionType == ActionType.Modify)
{
//这里应该update
this.NewRecipeAdd(); //更新配方数据到数据库
//this.WeighDelByRecipeID(txtRecipeId.Text); //清空配方对应的物料信息
this.NewWeighAdd(); //配方对应的物料信息重新插入数据库
}
else if(_actionType == ActionType.SaveAs)
{
this.NewRecipeAdd(); //新配方数据插入数据库
this.NewWeighAdd(); //配方对应的物料信息插入数据库
}
#endregion
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void DGVMaterialSet_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
//this.DGVMaterialSet.Refresh();
//int newRowNum = this.DGVMaterialSet.RowCount;
//if (pmt_Materials != null && pmt_Materials.Count > 0)
//{
// //(this.DGVMaterialSet.Rows[newRowNum - 1].Cells[0] as DataGridViewComboEditBoxCell).DataSource = pmt_Materials;
// foreach (Base_MaterialInfo pmt_Material in pmt_Materials)
// {
// (this.DGVMaterialSet.Rows[newRowNum - 1].Cells[0] as DataGridViewComboEditBoxCell).Items.Add(pmt_Material.materialName);
// }
//}
//this.DGVMaterialSet.Rows[newRowNum - 1].HeaderCell.Value = "+";
//this.DGVMaterialSet.CurrentCell = this.DGVMaterialSet[1, newRowNum - 1];
}
private void DGVMaterialSet_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//this.DGVMaterialSet.Rows.Add();
}
private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
{
//this.DGVMaterialSet.Rows.Add();
}
private void toolStripMenuItemDel_Click(object sender, EventArgs e)
{
//if(this.DGVMaterialSet.CurrentCell != null)
//{
// this.DGVMaterialSet.Rows.RemoveAt(this.DGVMaterialSet.CurrentRow.Index);
//}
}
#endregion
}
/// <summary>
/// 自定义可编辑下拉框单元
/// </summary>
public class DataGridViewComboEditBoxCell : DataGridViewComboBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
ComboBox comboBox = (ComboBox)base.DataGridView.EditingControl;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox.Validating += new CancelEventHandler(comboBox_Validating);
}
}
protected override object GetFormattedValue(object value, int rowIndex,ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter,TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value != null && value.ToString().Trim() != string.Empty)
{
if (Items.IndexOf(value) == -1)// 如果下拉框中不存在填入的值,则添加到下拉框中
{
//Items.Add(value);
//// 添加到该列所有单元所绑定的下拉列表中
//DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)OwningColumn;
//col.Items.Add(value);
return null;
}
}
return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
private void comboBox_Validating(object sender, CancelEventArgs e)
{
DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
if (cbo.Text.Trim() == string.Empty)
return;
DataGridView grid = cbo.EditingControlDataGridView;
object value = cbo.Text;
if (cbo.Items.IndexOf(value) == -1)
{
DataGridViewComboBoxColumn cboCol = (DataGridViewComboBoxColumn)grid.Columns[grid.CurrentCell.ColumnIndex];
grid.CurrentCell.Value = value;
}
}
}
public class DataGridViewComboEditBoxColumn : DataGridViewComboBoxColumn
{
public DataGridViewComboEditBoxColumn()
{
DataGridViewComboEditBoxCell obj = new DataGridViewComboEditBoxCell();
this.CellTemplate = obj;
}
}
}