2013-06-05 90 views
0

我在我的windows窗体上使用bindingsource当用户根据他的分期付款填写本金时,我计算他的每月金额。但问题是,当填写金额时我的文本更改事件触发两次,第二次principal为空。我如何解决完美的计算工作。Datagridview textchange event fire两次

private void prol04DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
     { 
     TextBox tx = e.Control as TextBox; 

     DataGridViewTextBoxCell cell = prol04DataGridView.CurrentCell as DataGridViewTextBoxCell; 


      if (tx != null && cell.OwningColumn == prol04DataGridView.Columns[5]) 
      { 
       tx.TextChanged -= new EventHandler(tx_TextChanged); 
       tx.TextChanged += new EventHandler(tx_TextChanged); 
      } 

     } 

这是我的文字改变事件

void tx_TextChanged(object sender, EventArgs e) 
    { 

    try 
    { 
     TextBox Principal = (TextBox)sender; 
     Prol04 _ded = (Prol04)prol04BindingSource.Current; 
     /* Here P is PrinciplAmount*/ 
     P = Convert.ToDecimal(Principal.Text); 
     Installment = Convert.ToDecimal(prol04DataGridView.CurrentRow.Cells[6].Value); 
     mnthPay = P/Installment; 
     _ded.MonthlyAmount = mnthPay; 
     _ded.Instalments =Convert.ToInt32(Installment); 
     prol04DataGridView.Refresh(); 

    } 

    catch (Exception ex) 
     { 
     throw (ex); 
    } 
} 
![Datagrid Where i perform event][1] 
+0

它会触发在控制中的每一个文本变化 – V4Vendetta

+0

没有在第一个功能我有控制,它将在本金量的单元格上工作 – Abhishek

+0

@Abhishek我试过你的代码,它调用一次事件,而不是两次。 ... – vikas

回答

0

尝试使用Datagridvirew.CellValueChanged,更换任何你想在tx_TextChanged事件做。

+0

这是winform而不是ASP – Abhishek

+0

它在form_load那么还是在设计器模式下 –

+0

更新的解决方案 –