Datagridview textchange事件触发两次

本文关键字:两次 textchange 事件 Datagridview | 更新日期: 2023-09-27 18:16:53

我在我的windows窗体上使用bindingsource,当用户根据他的分期付款填写本金金额时,我计算他的每月金额。但问题是,当填写金额时,我的文本更改事件发生两次,第二次principal使空。我怎样才能解决这个计算工作完美。

 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]

Datagridview textchange事件触发两次

尝试使用Datagridvirew.CellValueChanged,以取代您想要在tx_TextChanged事件中做的任何事情