DataGridView 在下拉框添加下来事件

DataGridView中有一种下拉框式的列,给这个列添加下拉事件时需要转化一下才可以绑定下拉事件

/// <summary>
/// 服务类型
/// </summary>
DataGridViewComboBoxEditingControl cboServiceType = null;



#region 维修项目 服务类型 下拉框 事件 /// <summary> /// 设置下拉框值改变时的处理 /// 作者“;李德彦 /// 时间:2015年12月7日 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgv_tb_maintain_three_guaranty_item_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { if (dgv_tb_maintain_three_guaranty_item.CurrentCell.OwningColumn == dgv_cbx1_WORK_SERVICE_TYPE) { cboServiceType = (DataGridViewComboBoxEditingControl)e.Control; cboServiceType.SelectedIndexChanged += new EventHandler(cboServiceType_SelectedIndexChanged); } } catch (Exception ex) { GlobalStaticObj.GlobalLogService.WriteLog(LocalOperation.GetStringByKey("sanbaofuwudantianjiamingxiyichang", "三包服务单,添加明细事件异常:") + ex.Message); } } /// <summary> /// 服务类型改变事件 /// 作者:李德彦 /// 时间:2015年12月7日 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void cboServiceType_SelectedIndexChanged(object sender, EventArgs e) { if (cboServiceType == null) { return; } if (cboServiceType.SelectedItem != null) { //当服务类型为“标准服务”时,行金额显示为具体数值,当服务类型为“宇通服务”和“其他服务”时,行金额显示0 DataRowView dr = (DataRowView)cboServiceType.SelectedItem; dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_cbx1_WORK_SERVICE_TYPE.Name].Value = dr["dic_name"]; dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_cbx1_WORK_SERVICE_TYPE.Name].Tag = dr["dic_code"]; if (dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_cbx1_WORK_SERVICE_TYPE.Name].Tag.ToString() == DbDic2Enum.OSN_SERVICE_TYPE_01)//标准服务 { //工时单价 string strfixed = string.Empty; if (dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_FIXED.Name].Value!=null && !string.IsNullOrEmpty(dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_FIXED.Name].Value.ToString())) { strfixed = dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_FIXED.Name].Value.ToString(); } string strtime = string.Empty; if (dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_TIME.Name].Value!= null && !string.IsNullOrEmpty(dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_TIME.Name].Value.ToString())) { //工时 strtime = dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_TIME.Name].Value.ToString(); } double doufixid, doutime,douretult = 0.00; if(double.TryParse(strfixed,out doufixid) && double.TryParse(strtime,out doutime)) { douretult = doufixid * doutime; } //行金额显示为具体数值 不需要保存两位小数,不需要四舍五入 dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_AMOUNT.Name].ReadOnly = true; dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_AMOUNT.Name].Value = douretult; } else { //行金额显示0 dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_AMOUNT.Name].Value = 0; dgv_tb_maintain_three_guaranty_item.CurrentRow.Cells[dgv_txt1_WORK_AMOUNT.Name].ReadOnly = true; } } } /// <summary> /// 结束编辑 /// 作者:李德彦 /// 时间:2015年12月7日 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgv_tb_maintain_three_guaranty_item_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { if (cboServiceType != null) { cboServiceType.SelectedValueChanged -= new EventHandler(cboServiceType_SelectedIndexChanged); cboServiceType = null; } } catch (Exception ex) { GlobalStaticObj.GlobalLogService.WriteLog(LocalOperation.GetStringByKey("sanbaofuwuzengjiachuangtidanyuangejieshuyichang", "三包服务增加窗体,单元格结束编辑事件异常:") + ex.Message); } } #endregion

  

posted @ 2016-02-27 17:01  yzhddeyan  阅读(912)  评论(0编辑  收藏  举报