/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:F_PM_0901.cs
* 2.功能描述:工序检验
* 编辑履历:
* 作者 日期 版本 修改内容
* 王鑫 2014/09/28 1.00 新建
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseControls;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.Library;
using Dongke.IBOSS.PRD.Client.CommonModule;
using Dongke.IBOSS.PRD.WCF.DataModels;
using Dongke.IBOSS.PRD.WCF.Proxys;
using Dongke.IBOSS.PRD.WCF.Proxys.PMModuleService;
using Dongke.IBOSS.PRD.WCF.Proxys.SystemModuleService;
namespace Dongke.IBOSS.PRD.Client.PMModule
{
///
/// 工序检验一览
///
public partial class F_PM_0902 : FormBase
{
#region 成员变量
// 工序ID
private int _procedureID;
// 生产工号
private int _userID = -1;
// 生产编号
private string _userCode;
// 生产名称
private string _userName;
// 工序实体类
ProcedureEntity _procedureDataEntity;
// 产品数据表
DataTable _dtProductionData = null;
// 产品缺陷数据表
DataTable _dtProductionDefectData = null;
// 当前选择的行
private int _selectedRowIndex;
// ComBoxEditingControl
private DataGridViewComboBoxEditingControl _dataGridViewComboBox = null;
// 当前产品条码的产品ID,用来查询缺陷位置
private int _currentGoodsID = 0;
// 当前产品条码,用来切换筛选列表数据
private string _currentBarCode = "";
// 返工工序搜索列表对象
C_DGV_Cell_ListBoxComboBox _C_DGV_Cell_ListBoxComboBox = null;
// 过滤结果集
private DataSet _ds = null;
// 过滤员工结果集
private DataSet _dsStaff = null;
// 过滤缺陷图片
private DataSet _dsImage = null;
// 临时数,用来过滤责任员工数据源标识
private int _tempCount = 0;
// 当前临时标记
private string _currentTempSign = "";
// 图片字节集
private List _PicByte = new List();
// 缩略图片字节集
private List _smallByte = new List();
#endregion
#region 属性
///
/// 生产工号
///
public int UserID
{
set
{
_userID = value;
}
get
{
return _userID;
}
}
///
/// 生产编号
///
public string UserCode
{
set
{
_userCode = value;
}
get
{
return _userCode;
}
}
///
/// 用户名称
///
public string UserName
{
set
{
_userName = value;
}
get
{
return _userName;
}
}
///
/// 产品数据表
///
public DataTable TableProductionData
{
set
{
_dtProductionData = value;
}
get
{
if (_dtProductionData == null)
{
_dtProductionData = new DataTable("ProductionData");
_dtProductionData.Columns.Add("BarCode");
_dtProductionData.Columns.Add("GoodsID");
_dtProductionData.Columns.Add("GoodsCode");
_dtProductionData.Columns.Add("GoodsName");
_dtProductionData.Columns.Add("DefectFlagID");
_dtProductionData.Columns.Add("ReworkProcedureID");
_dtProductionData.Columns.Add("Remarks");
_dtProductionData.Columns.Add("UserID");
return _dtProductionData;
}
else
{
return _dtProductionData;
}
}
}
///
/// 产品缺陷表
///
public DataTable TableProductionDefectData
{
set
{
_dtProductionDefectData = value;
}
get
{
if (_dtProductionDefectData == null)
{
_dtProductionDefectData = new DataTable("ProductionDefectData");
_dtProductionDefectData.Columns.Add("ProductionBarCode");
_dtProductionDefectData.Columns.Add("discarde");
_dtProductionDefectData.Columns.Add("DefectFlag", typeof(decimal));
_dtProductionDefectData.Columns.Add("DefectLocation", typeof(decimal));
_dtProductionDefectData.Columns.Add("DutyProcedureUserID", typeof(decimal));
_dtProductionDefectData.Columns.Add("DutyUserCode");
_dtProductionDefectData.Columns.Add("DefectRemarks");
return _dtProductionDefectData;
}
else
{
return _dtProductionDefectData;
}
}
}
///
/// 缺陷数据集
///
public DataSet DSTable
{
set
{
_ds = value;
}
get
{
if (_ds == null)
{
_ds = new DataSet();
return _ds;
}
else
{
return _ds;
}
}
}
///
/// 员工数据集
///
public DataSet DSTableStaff
{
set
{
_dsStaff = value;
}
get
{
if (_dsStaff == null)
{
_dsStaff = new DataSet();
return _dsStaff;
}
else
{
return _dsStaff;
}
}
}
///
/// 缺陷图片
///
public DataSet DSTableImage
{
set
{
_dsImage = value;
}
get
{
if (_dsImage == null)
{
_dsImage = new DataSet();
return _dsImage;
}
else
{
return _dsImage;
}
}
}
#endregion
#region 构造函数
public F_PM_0902()
{
InitializeComponent();
}
public F_PM_0902(int procedureID, string fromTitle)
{
InitializeComponent();
this._procedureID = procedureID;
this.Text = fromTitle;
//this.tsbtnDelete.Text = Constant.BUTTON_TEXT_DELETE;
//自动适应列宽
//this.tsbtnAdaptive.Text = Constant.TOOLBAR_BUTTON_TEXT_AUTOCOLUMNWIDTH;
//关闭
//this.tsbtnClose.Text = Constant.TOOLBAR_BUTTON_TEXT_CLOSE;
//this.btnSave.Text = Constant.BUTTON_TEXT_SAVE;
//this.btnCancel.Text = Constant.BUTTON_TEXT_CANCEL;
}
#endregion
#region 事件
///
/// 窗体加载事件
///
///
///
private void F_PM_0901_Load(object sender, System.EventArgs e)
{
try
{
this.dgvProductionData.AutoGenerateColumns = false;
this.dgvProductionData.DataSource = TableProductionData;
this.dgvDefect.AutoGenerateColumns = false;
this.dgvDefectStaff.AutoGenerateColumns = false;
// 加载权限
FormPermissionManager.FormPermissionControl(this.Name, this,
Dongke.IBOSS.PRD.Client.DataModels.LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData,
Dongke.IBOSS.PRD.Client.DataModels.LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
// 获取工序明细数据
ProcedureEntity procedureDataEntity = (ProcedureEntity)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetProcedureDataEntityByID(this._procedureID);
}));
this._procedureDataEntity = procedureDataEntity;
DataSet dsDefectByProcedure = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDefectByProcedure(this._procedureID);
}));
if (dsDefectByProcedure != null && dsDefectByProcedure.Tables.Count > 0
&& dsDefectByProcedure.Tables[0].Rows.Count > 0)
{
this.Column1.DisplayMember = "DEFECTFLAGNAME";
this.Column1.ValueMember = "DEFECTFLAGID";
this.Column1.DataSource = dsDefectByProcedure.Tables[0].Copy();
}
// 缺陷数据源
this.DefectFlag.DisplayMember = "DefectFlagName";
this.DefectFlag.ValueMember = "DefectFlagID";
this.DefectFlag.DataSource = CreateDataSource.GetDefectFlagTable();
//DataTable dtReworkProcedure = new DataTable(); // 默认返回工序数据源,为空
//dtReworkProcedure.Columns.Add("ReworkProcedureID", typeof(decimal));
//dtReworkProcedure.Columns.Add("ReworkProcedureName");
//this.ReworkProcedureID.DisplayMember = "ReworkProcedureName";
//this.ReworkProcedureID.ValueMember = "ReworkProcedureID";
//this.ReworkProcedureID.DataSource = dtReworkProcedure;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 工号文本离开焦点后事件
///
///
///
{
return PMModuleProxy.Service.CheckProcedureUser(this._procedureID, this.txtUserCode.Text.Trim());
}));
if (MsgCheckProcedureUser != null)
{
if (MsgCheckProcedureUser.ErrMsg != null)
{
MessageBox.Show(MsgCheckProcedureUser.ErrMsg,
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.txtUserCode.Focus();
return;
}
UserID = Convert.ToInt32(MsgCheckProcedureUser.UserID);
UserCode = MsgCheckProcedureUser.UserCode;
UserName = MsgCheckProcedureUser.UserName;
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 条形码按键事件
///
///
///
private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
if ((int)e.KeyChar == 13) // 按了回车键
{
if (this.txtBarCode.Text.Trim() == string.Empty)
{
this.txtBarCode.Focus();
return;
}
// 校验条码 获取校验条码信
DataSet dsCheckBarcode = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.CheckBarcode(this._procedureID, this.txtBarCode.Text.Trim());
}));
if (dsCheckBarcode != null && dsCheckBarcode.Tables[0].Rows.Count > Constant.INT_IS_ZERO)
{
if (dsCheckBarcode.Tables[0].Rows[0][Constant.BarCodeResultTableColumns.out_errMsg.ToString()].ToString() != string.Empty)
{
// 表示有错误
MessageBox.Show(string.Format(dsCheckBarcode.Tables[0].Rows[0][Constant.BarCodeResultTableColumns.out_errMsg.ToString()].ToString(), this.txtBarCode.Text.Trim(), "产品条码"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.txtBarCode.Focus();
return;
}
else
{
BindProductionData(dsCheckBarcode);
this._currentBarCode = txtBarCode.Text.Trim();
BindDefectLocation(); // 绑定缺陷位置
this.txtBarCode.Text = string.Empty;
FilterTableRow();
}
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
#endregion
#region 绑定到第一个DataGriwView数据源
///
/// 绑定到第一个DataGriwView数据源
///
private void BindProductionData(DataSet dsCheckBarcode)
{
try
{
DataRow[] dr = TableProductionData.Select("BarCode='" + this.txtBarCode.Text.Trim() + "'");
if (dr.Length > 0)
{
// 不允许重复添加
return;
}
DataRow drNew = TableProductionData.NewRow();
drNew["BarCode"] = this.txtBarCode.Text.Trim();
drNew["GoodsID"] = dsCheckBarcode.Tables[0].Rows[0][Constant.BarCodeResultTableColumns.out_goodsID.ToString()];
drNew["GoodsCode"] = dsCheckBarcode.Tables[0].Rows[0][Constant.BarCodeResultTableColumns.out_goodsCode.ToString()];
drNew["GoodsName"] = dsCheckBarcode.Tables[0].Rows[0][Constant.BarCodeResultTableColumns.out_goodsName.ToString()];
drNew["DefectFlagID"] = 0; ;
// drNew["ReworkProcedureID"] = 0;
drNew["ReworkProcedureID"] = null;
drNew["Remarks"] = "";
drNew["UserID"] = UserID;
TableProductionData.Rows.Add(drNew);
if (DSTable.Tables.IndexOf(this.txtBarCode.Text.Trim()) == -1)
{
DataTable dtProductionDefectData = new DataTable(this.txtBarCode.Text.Trim());
dtProductionDefectData.Columns.Add("ProductionBarCode");
dtProductionDefectData.Columns.Add("discarde");
dtProductionDefectData.Columns.Add("DefectFlag", typeof(decimal));
dtProductionDefectData.Columns.Add("DefectLocation", typeof(decimal));
dtProductionDefectData.Columns.Add("DutyProcedureID", typeof(decimal));
dtProductionDefectData.Columns.Add("DutyProcedureUserID", typeof(decimal));//责任员工
dtProductionDefectData.Columns.Add("Jobs", typeof(decimal));//工种
dtProductionDefectData.Columns.Add("DefectRemarks");
dtProductionDefectData.Columns.Add("TempCount");
dtProductionDefectData.Columns.Add("ProductionDataID"); //生产数据ID
DSTable.Tables.Add(dtProductionDefectData);
}
this.dgvDefect.DataSource = DSTable.Tables[this.txtBarCode.Text.Trim()];
this.dgvProductionData.CurrentCell = null;
this.dgvProductionData.Rows[dgvProductionData.RowCount - 1].Selected = true;
this.dgvDefect.ReadOnly = true;
this.dgvDefectStaff.ReadOnly = true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 私有方法
///
/// 通过是否有缺陷来启用或禁用
///
///
private void SetGridView(int DefectFlag)
{
if (DefectFlag == 0) //没有缺陷
{
dgvDefect.ReadOnly = true;
dgvDefectStaff.ReadOnly = true;
}
else
{
dgvDefect.ReadOnly = false; ;
dgvDefectStaff.ReadOnly = false;
}
}
#endregion
///
/// 生产数据编辑控件显示事件
///
///
///
private void dgvProductionData_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl
&& this.dgvProductionData.CurrentCell.ColumnIndex == 3
&& this.dgvProductionData.CurrentCell.RowIndex != -1)
{
this._dataGridViewComboBox = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
_dataGridViewComboBox.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
}
}
///
/// 生产数据下拉列表改变事件
///
///
///
private void dataGridViewComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
SetGridView(Convert.ToInt32(((ComboBox)sender).SelectedValue.ToString()));
}
///
/// 产品数据单元格进入事件
///
///
///
private void dgvProductionData_CellEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
if (this.dgvProductionData.CurrentCell != null)
{
// 记录最后选择行
this._selectedRowIndex = this.dgvProductionData.CurrentCell.RowIndex;
_currentBarCode = this.dgvProductionData.Rows[_selectedRowIndex].Cells["BarCode"].Value.ToString();
_C_DGV_Cell_ListBoxComboBox = dgvProductionData.Rows[_selectedRowIndex].Cells[4] as C_DGV_Cell_ListBoxComboBox;
DataSet ds = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetReworkProcedureByBarcode(this._procedureID, this.dgvProductionData.Rows[_selectedRowIndex].Cells["BarCode"].Value.ToString());
}));
_currentGoodsID = Convert.ToInt32(this.dgvProductionData.Rows[_selectedRowIndex].Cells["GoodsID"].Value);
_currentBarCode = this.dgvProductionData.Rows[_selectedRowIndex].Cells["BarCode"].Value.ToString();
_C_DGV_Cell_ListBoxComboBox.DisplayMember = "ReworkProcedureName";
_C_DGV_Cell_ListBoxComboBox.ValueMember = "ReworkProcedureID";
_C_DGV_Cell_ListBoxComboBox.DataSource = ds.Tables[0];
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 产品数据选定项改变事件
///
///
///
private void dgvProductionData_SelectionChanged(object sender, EventArgs e)
{
try
{
if (this.dgvProductionData.CurrentCell != null)
{
SetGridView(Convert.ToInt32(this.dgvProductionData.Rows[_selectedRowIndex].Cells["DefectFlag"].Value));
FilterTableRow();
BindDefectLocation();
this.dgvDefectStaff.DataSource = null;
_smallByte.Clear();//防止重复绑定图片
this.lvPic.Clear();//
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 保存按钮事件
///
///
///
private void btnSave_Click(object sender, EventArgs e)
{
DataTable dt = this.TableProductionData;
DataTable dt2 = this.TableProductionDefectData;
DataSet ds = DSTable;
}
///
/// 绑定缺陷数据源
///
private void BindDefectDataSource()
{
try
{
DataSet dsDefectByProcedure = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDefectByProcedure(this._procedureID);
}));
if (dsDefectByProcedure != null && dsDefectByProcedure.Tables.Count > 0
&& dsDefectByProcedure.Tables[0].Rows.Count > 0)
{
// 缺陷数据源
this.Column1.DisplayMember = "DefectFlagName";
this.Column1.ValueMember = "DefectFlagID";
this.Column1.DataSource = dsDefectByProcedure.Tables[0];
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
///
///
private void FilterTableRow()
{
this.dgvDefect.DataSource = DSTable.Tables[_currentBarCode];
if (this.dgvDefect.DataSource != null)
{
if (DSTable.Tables[_currentBarCode].Rows.Count > 0)
{
for (int i = 0; i < DSTable.Tables[_currentBarCode].Rows.Count; i++)
{
string currentBarCode = DSTable.Tables[_currentBarCode].Rows[i]["ProductionBarCode"].ToString();
if (DSTable.Tables[_currentBarCode].Rows[i]["DefectFlag"].ToString() != "")
{
int defectid = Convert.ToInt32(DSTable.Tables[_currentBarCode].Rows[i]["DefectFlag"].ToString());
DataGridViewComboBoxCell aa = dgvDefect.Rows[i].Cells[3] as DataGridViewComboBoxCell;
DataSet dsDutyDefect = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDutyProcedureByBarCode(currentBarCode, defectid);
}));
aa.DisplayMember = "DutyProcedureName";
aa.ValueMember = "DutyProcedureID";
aa.DataSource = dsDutyDefect.Tables[0];
}
}
}
}
}
private void dgvDefect_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
this.dgvDefect.Rows[e.Row.Index - 1].Cells["ProductionBarCode"].Value = _currentBarCode;
this.dgvDefect.Rows[e.Row.Index - 1].Cells["TempCount"].Value = (this._tempCount++);
// 添加行后,默认创建对应的临时表
DataTable dtStaff = new DataTable(string.Format("TempTable{0}", this.dgvDefect.Rows[e.Row.Index - 1].Cells["TempCount"].Value.ToString()));
dtStaff.Columns.Add("IsSelected");
dtStaff.Columns.Add("StaffID");
dtStaff.Columns.Add("StaffCode");
dtStaff.Columns.Add("StaffName");
dtStaff.Columns.Add("StaffTempCount");
DSTableStaff.Tables.Add(dtStaff);
this.dgvDefectStaff.DataSource = null; // 创建新行后,责任员工列表清空
// 添加行后,默认创建对应的缺陷图片临时表
DataTable dtImage = new DataTable(string.Format("TempTable{0}", this.dgvDefect.Rows[e.Row.Index - 1].Cells["TempCount"].Value.ToString()));
dtImage.Columns.Add("StaffTempCount");
dtImage.Columns.Add("ImageByte", typeof(byte[]));
DSTableImage.Tables.Add(dtImage);
this.lvPic.Items.Clear();//当前上传的图片显示清空
}
///
/// 单元格CheckBox事件
///
///
///
private void dgvDefect_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
int checkValue = Convert.ToInt32(this.dgvDefect.Rows[e.RowIndex].Cells[0].EditedFormattedValue);
SetCheckBoxSelected(checkValue, e.RowIndex);
}
}
///
/// 清空以前所选中的缺陷
///
///
private void SetCheckBoxSelected(int checkValue, int rowIndex)
{
DataTable dtOrgDataSource = this.dgvDefect.DataSource as DataTable;
for (int i = 0; i < DSTable.Tables[dtOrgDataSource.TableName].Rows.Count; i++)
{
if (i == rowIndex)
{
dtOrgDataSource.Rows[i]["discarde"] = 1;
}
else
{
dtOrgDataSource.Rows[i]["discarde"] = 0;
}
}
}
///
/// 绑定缺陷位置
///
private void BindDefectLocation()
{
int goodsID = 0;
DataRow[] dr = TableProductionData.Select("BarCode='" + this._currentBarCode + "'");
if (dr.Length > 0)
{
goodsID = Convert.ToInt32(dr[0]["GoodsID"]);
}
DataSet dsDefectLocation = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDefectLocaionByGoodsID(goodsID);
}));
this.DefectLocationColumn.DisplayMember = "DEFECTPOSITIONNAME";
this.DefectLocationColumn.ValueMember = "DEFECTPOSITIONID";
this.DefectLocationColumn.DataSource = dsDefectLocation.Tables[0];
}
///
/// 绑定责任工序
///
/// 产品条码
/// 责任工序
private void BindDutyDefect(string barcode, int defectid)
{
DataSet dsDutyDefect = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDutyProcedureByBarCode(barcode, defectid);
}));
if (dsDutyDefect != null && dsDutyDefect.Tables.Count > 0
&& dsDutyDefect.Tables[0].Rows.Count > 0)
{
this.DutyProcedureIDLocation.DisplayMember = "ProcedureName";
this.DutyProcedureIDLocation.ValueMember = "ProcedureID";
this.DutyProcedureIDLocation.DataSource = dsDutyDefect.Tables[0];//DutyProcedure
}
}
///
/// 缺陷数据下拉列表改变事件
///
///
///
private void dataGridViewComboBox_DutyDefect_SelectionChangeCommitted(object sender, EventArgs e)
{
if (((ComboBox)sender).SelectedValue != null)
{
BindDutyDefect(_currentBarCode, Convert.ToInt32(((ComboBox)sender).SelectedValue.ToString()));
}
}
///
/// 缺陷产品单元格进入事件
///
///
///
private void dgvDefect_CellEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
if (this.dgvDefect.CurrentCell != null)
{
// 记录最后选择行
//this.dgvDefect.CurrentCell.RowIndex;
if (this.dgvDefect.CurrentCell.ColumnIndex == 3
&& this.dgvDefect.CurrentCell.RowIndex != -1)
{
string currentBarCode = this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["ProductionBarCode"].Value.ToString();
if (this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["Column1"].Value.ToString() != "")
{
int defectid = Convert.ToInt32(this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["Column1"].Value.ToString());
DataGridViewComboBoxCell aa = dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells[3] as DataGridViewComboBoxCell;
DataSet dsDutyDefect = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDutyProcedureByBarCode(currentBarCode, defectid);
}));
aa.DisplayMember = "DutyProcedureName";
aa.ValueMember = "DutyProcedureID";
aa.DataSource = dsDutyDefect.Tables[0];
}
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 根据责任工序读出责任员工,并绑定到数据源
///
///
///
private void SetDutyCode(string barcode, int dutyProcedureID, int rowIndex)
{
DataSet dsDutyCode = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDutyProcedureCodeByBarCode(barcode, dutyProcedureID);
}));
if (dsDutyCode != null && dsDutyCode.Tables.Count > 0
&& dsDutyCode.Tables[0].Rows.Count > 0)
{
this.DutyProcedureCodeLocation.DisplayMember = "UserCode";
this.DutyProcedureCodeLocation.ValueMember = "UserID";
this.DutyProcedureCodeLocation.DataSource = dsDutyCode.Tables[0];
this.dgvDefect.CurrentCell = null;
this.dgvDefect.Rows[rowIndex].Cells["DutyProcedureCodeLocation"].Value = dsDutyCode.Tables[0].Rows[0]["UserID"].ToString();
this.dgvDefect.Rows[rowIndex].Cells["ProductionDataID"].Value = dsDutyCode.Tables[0].Rows[0]["ProductionDataID"].ToString();
SetDutyJobs(Convert.ToInt32(dsDutyCode.Tables[0].Rows[0]["UserID"]), dutyProcedureID, rowIndex, Convert.ToInt32(dsDutyCode.Tables[0].Rows[0]["ProductionDataID"]));
}
}
///
/// 根据责任工序读出工种,并绑定到数据源
///
private void SetDutyJobs(int UserID, int dutyProcedureID, int rowIndex, int ProductionDataID)
{
DataSet dsDutyJobs = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDutyJobsCodeByUser(UserID, ProductionDataID);
}));
if (dsDutyJobs != null && dsDutyJobs.Tables.Count > 0
&& dsDutyJobs.Tables[0].Rows.Count > 0)
{
this.JobsLocation.DisplayMember = "JobsName";
this.JobsLocation.ValueMember = "JobsID";
this.JobsLocation.DataSource = dsDutyJobs.Tables[0];
this.dgvDefect.CurrentCell = null;
this.dgvDefect.Rows[rowIndex].Cells["JobsLocation"].Value = dsDutyJobs.Tables[0].Rows[0]["JobsID"].ToString();
_currentTempSign = this.dgvDefect.Rows[rowIndex].Cells["TempCount"].Value.ToString(); //当前临时标记
// 通过工种来搜索缺陷责任员工列表
BindDutyStaff(ProductionDataID, UserID, Convert.ToInt32(dsDutyJobs.Tables[0].Rows[0]["JobsID"]), Convert.ToInt32(this.dgvDefect.Rows[rowIndex].Cells["TempCount"].Value));
}
}
///
/// 缺陷产品当前是否有未提交数据事件
///
///
///
private void dgvDefect_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
try
{
if (this.dgvDefect.CurrentCell != null)
{
// 记录最后选择行
if (this.dgvDefect.CurrentCell.ColumnIndex == 3
&& this.dgvDefect.CurrentCell.RowIndex != -1)
{
if (dgvDefect.IsCurrentCellDirty)
{
dgvDefect.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 缺陷单元格值改变事件
///
///
///
private void dgvDefect_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
if (this.dgvDefect.CurrentCell != null)
{
if (this.dgvDefect.CurrentCell.ColumnIndex == 3
&& this.dgvDefect.CurrentCell.RowIndex != -1)
{
string currentBarCode = this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["ProductionBarCode"].Value.ToString();
if (this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["DutyProcedureIDLocation"].Value.ToString() != "")
{
int dutyProcedureID = Convert.ToInt32(this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["DutyProcedureIDLocation"].Value.ToString());
SetDutyCode(currentBarCode, dutyProcedureID, this.dgvDefect.CurrentCell.RowIndex);
}
}
else if (this.dgvDefect.CurrentCell.ColumnIndex == 4
&& this.dgvDefect.CurrentCell.RowIndex != -1)
{
if (this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["DutyProcedureIDLocation"].Value.ToString() != ""
&& this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["DutyProcedureCodeLocation"].Value.ToString() != "")
{
int userID = Convert.ToInt32(this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["DutyProcedureCodeLocation"].Value.ToString());
int dutyProcedureID = Convert.ToInt32(this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["DutyProcedureIDLocation"].Value.ToString());
int productionDataID = Convert.ToInt32(this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["ProductionDataID"].Value.ToString());
SetDutyJobs(userID, dutyProcedureID, this.dgvDefect.CurrentCell.RowIndex, productionDataID);
}
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 绑定缺陷责任员工
///
/// 生产数据ID
/// 用户ID
/// 工种ID
/// 唯一标识
private void BindDutyStaff(int productionDataID, int userID, int jobs, int StaffTempCount)
{
// 获取临时表是否有数据,如果有行,则删除,
DataTable dtTemp = DSTableStaff.Tables[string.Format("TempTable{0}", StaffTempCount.ToString())];
if (dtTemp.Rows.Count > 0)
{
// 清空临时表数据
DSTableStaff.Tables[string.Format("TempTable{0}", StaffTempCount.ToString())].Clear();
}
DataSet dsDutyStaff = (DataSet)DoAsync(new BaseAsyncMethod(() =>
{
return PMModuleProxy.Service.GetDutyStaffByUserIDAndJobs(productionDataID, userID, jobs);
}));
if (dsDutyStaff != null && dsDutyStaff.Tables.Count > 0
&& dsDutyStaff.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in dsDutyStaff.Tables[0].Rows)
{
DataRow addDr = DSTableStaff.Tables[string.Format("TempTable{0}", StaffTempCount.ToString())].NewRow();
addDr["IsSelected"] = 0;
addDr["StaffID"] = dr["StaffID"];
addDr["StaffCode"] = dr["StaffCode"];
addDr["StaffName"] = dr["StaffName"];
addDr["StaffTempCount"] = StaffTempCount;
DSTableStaff.Tables[string.Format("TempTable{0}", StaffTempCount.ToString())].Rows.Add(addDr);
}
this.dgvDefectStaff.DataSource = DSTableStaff.Tables[string.Format("TempTable{0}", StaffTempCount.ToString())];
}
}
///
/// 缺陷数据源选定项改变事件
///
///
///
private void dgvDefect_SelectionChanged(object sender, EventArgs e)
{
try
{
if (this.dgvDefect.CurrentCell != null)
{
if (this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["TempCount"].Value.ToString() != string.Empty)
{
int staffTempCount = Convert.ToInt32(this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["TempCount"].Value.ToString());
this.dgvDefectStaff.DataSource = DSTableStaff.Tables[string.Format("TempTable{0}", staffTempCount.ToString())];
_currentTempSign = this.dgvDefect.Rows[this.dgvDefect.CurrentCell.RowIndex].Cells["TempCount"].Value.ToString(); //当前临时标记
_smallByte.Clear();//防止重复绑定图片
BindByteImage(DSTableImage.Tables[string.Format("TempTable{0}", staffTempCount.ToString())]);
}
else
{
this.dgvDefectStaff.DataSource = null;
_smallByte.Clear();//防止重复绑定图片
this.lvPic.Clear();//
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 绑定并显示图片
///
///
protected void BindByteImage(DataTable ImageData)
{
_smallByte.Clear();
foreach (DataRow dr in ImageData.Rows)
{
//将数据库中的缩略图取出
_smallByte.Add((byte[])dr[1]);
}
//绑定缩略图
BindImg();
}
///
/// 上传图片按钮事件
///
///
///
private void btnUpload_Click(object sender, EventArgs e)
{
try
{
this.odlgFile.Filter = Constant.FILTER_PIC;
this.odlgFile.FilterIndex = Constant.INT_IS_ZERO;
this.odlgFile.RestoreDirectory = true;
this.odlgFile.Title = "选择缺陷图片";
this.odlgFile.FileName = null;
this.odlgFile.RestoreDirectory = true;
if (this.odlgFile.ShowDialog() == DialogResult.OK)
{
FileInfo file = new FileInfo(odlgFile.FileName);
if (Constant.UPLOAD_PIC_MAX_SIZE < file.Length)
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W013, "缺陷图片", "大小", "1M"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Image PicImage = Image.FromStream(file.OpenRead());
this._PicByte.Add(ImageToByte(PicImage));
GetThumbnail(file);
BindImg();
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 将图片文件转换成二进制
///
///
///
public static byte[] ImageToByte(Image img)
{
byte[] smallbuffer = null;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = Constant.INT_IS_ZERO;
smallbuffer = new byte[ms.Length];
ms.Read(smallbuffer, Constant.INT_IS_ZERO, Convert.ToInt32(ms.Length));
ms.Flush();
}
return smallbuffer;
}
///
/// 重绘缩略图并把缩略图转为二进制储存
///
/// 图片源路径
/// 缩放后图片高度
/// 缩放后图片宽度
///
public void GetThumbnail(FileInfo sourceFile)
{
Image imgSource = Image.FromStream(sourceFile.OpenRead());
ImageFormat thisFormat = imgSource.RawFormat;
int sW = 0, sH = 0;
// 按比例缩放
int sWidth = imgSource.Width;
int sHeight = imgSource.Height;
int destWidth = 100;
int destHeight = getSmallImageHeight(sWidth, sHeight, destWidth);
if (sHeight > destHeight || sWidth > destWidth)
{
if ((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
sW = sWidth;
sH = sHeight;
}
Bitmap outBmp = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.Black);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//将重绘的图片转为二进制并保存
Image image = (Image)outBmp;
byte[] smallbuffer = ImageToByte(image);
if (this._currentTempSign != "")
{
DataRow dr = DSTableImage.Tables[string.Format("TempTable{0}", this._currentTempSign)].NewRow();
dr["StaffTempCount"] = this._currentTempSign;
dr["ImageByte"] = smallbuffer;
DSTableImage.Tables[string.Format("TempTable{0}", this._currentTempSign)].Rows.Add(dr);
}
_smallByte.Add(smallbuffer);
imgSource.Dispose();
outBmp.Dispose();
}
///
/// 根据原图片宽高比获取缩略图的高(根据宽)
///
///
///
///
///
protected int getSmallImageHeight(int BigWidth, int BigHeight, int SmallWidth)
{
decimal scale = Convert.ToDecimal(BigWidth) / Convert.ToDecimal(BigHeight);
return Convert.ToInt32(SmallWidth / scale);
}
///
/// 绑定缩略图到控件中
///
protected void BindImg()
{
ImageList ilPic = new ImageList();
//每次绑定要清空数据源
ilPic.Images.Clear();
//将缩略图二进制集合中的数据转换成图片文件
List LSImageList = new List();
foreach (byte[] smallby in _smallByte)
{
LSImageList.Add(byteArrayToImage(smallby));
}
//添加数据源
foreach (Image img in LSImageList)
{
ilPic.Images.Add(img);
}
ilPic.ImageSize = new Size(100, 100);
this.lvPic.LargeImageList = ilPic;
this.lvPic.BeginUpdate();
//清空列表的数据源
lvPic.Items.Clear();
//添加列表的数据源
for (int i = 0; i < ilPic.Images.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.ImageIndex = i;
this.lvPic.Items.Add(lvi);
}
this.lvPic.EndUpdate();
}
///
/// 将数据库中的二进制转换成图片
///
///
///
public static Image byteArrayToImage(object data)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])data);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
return returnImage;
}
///
/// 删除图片按钮事件
///
///
///
private void btnDelete_Click(object sender, EventArgs e)
{
foreach (ListViewItem lvSelect in lvPic.SelectedItems)
{
int index = lvSelect.Index;
if (index < Constant.INT_IS_ZERO)
{
index = Constant.INT_IS_ZERO;
}
if (this._currentTempSign != "")
{
DSTableImage.Tables[string.Format("TempTable{0}", this._currentTempSign)].Rows.RemoveAt(lvSelect.Index);
DSTableImage.Tables[string.Format("TempTable{0}", this._currentTempSign)].AcceptChanges();
}
BindByteImage(DSTableImage.Tables[string.Format("TempTable{0}", this._currentTempSign)]);
}
}
///
/// 关闭按钮事件
///
///
///
private void tsbtnClose_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 产品条码列表删除事件
///
///
///
private void dgvProductionData_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
try
{
string BarCode = this.dgvProductionData.Rows[e.Row.Index].Cells["BarCode"].Value.ToString();
DSTable.Tables[BarCode].Rows.Clear();//清空行
this.dgvDefectStaff.DataSource = null;
this.lvPic.Clear();
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 产品缺陷删除行事件
///
///
///
private void dgvDefect_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
try
{
string TempCount = this.dgvDefect.Rows[e.Row.Index].Cells["TempCount"].Value.ToString();
DSTableStaff.Tables[string.Format("TempTable{0}", TempCount.ToString())].Rows.Clear();//清空行
this.dgvDefectStaff.DataSource = null;
this.lvPic.Clear();
}
catch (Exception ex)
{
throw ex;
}
}
}
}