我有一個datagridview我從數據庫中加載數據。該datagridview有2個int列,我插入數字。而且,如果我在兩列之一中插入一個數字,還有兩列單元格會自動填充數學運算的結果。相反,如果我插入一個字母,datagridview會顯示一個錯誤。DataGridview錯誤後CurrentCellDirtyStateChanged不工作C#
問題是這樣的:每當datagridview顯示錯誤時,單擊錯誤消息框中的「確定」後,我可以繼續編輯單元格,但CurrentCellDirtyStateChanged事件不起作用。因此其他兩列單元格不會自動填充。我需要他們在顯示錯誤後自動填寫。
這是我的代碼:
private void dgvReciboPagoSalario_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.ThrowException = false;
string Error = "Error: La columna " +
dgvReciboPagoSalario.Columns[e.ColumnIndex].HeaderText +
" es de tipo numérico";
DialogResult DR= MessageBox.Show(Error, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel = false;
}
這是CurrentCellDirtyStateChanged事件:
private void dgvReciboPagoSalario_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{//modificar campos de dgv al mismo tiempo que cambio otro
int HO = 0;
int HF = 0;
long SXH = 0;
long Subtotal = 0;
long DeduccionesPrestamo = 0;
long DeduccionesCCSS = 0;
long Total = 0;
if (dgvReciboPagoSalario.IsCurrentCellDirty)
{
dgvReciboPagoSalario.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (!string.IsNullOrEmpty(dgvReciboPagoSalario.CurrentRow.Cells["Horas Ordinarias"].Value.ToString()))
{
HO = Convert.ToInt32(dgvReciboPagoSalario.CurrentRow.Cells["Horas Ordinarias"].Value);
}
if (!string.IsNullOrEmpty(dgvReciboPagoSalario.CurrentRow.Cells["Horas Feriado"].Value.ToString()))
{
HF = Convert.ToInt32(dgvReciboPagoSalario.CurrentRow.Cells["Horas Feriado"].Value);
}
SXH = Convert.ToInt64(dgvReciboPagoSalario.CurrentRow.Cells["Salario por Hora"].Value);
Subtotal = ((HO * SXH) + ((SXH * 2) * HF));
dgvReciboPagoSalario.CurrentRow.Cells["Subtotal Recibido"].Value = Subtotal;
if (!string.IsNullOrEmpty(dgvReciboPagoSalario.CurrentRow.Cells["Deducciones Préstamo"].Value.ToString()))
{
DeduccionesPrestamo = Convert.ToInt64(dgvReciboPagoSalario.CurrentRow.Cells["Deducciones Préstamo"].Value);
}
DeduccionesCCSS = Convert.ToInt64(dgvReciboPagoSalario.CurrentRow.Cells["Deducciones CCSS"].Value);
Total = (Subtotal - (DeduccionesCCSS + DeduccionesPrestamo));
dgvReciboPagoSalario.CurrentRow.Cells["Total Recibido"].Value = Total;
}
在此先感謝!