【发布时间】:2017-05-27 04:31:38
【问题描述】:
我有一个datagridview,我从数据库中加载数据。该 datagridview 有 2 个 int 列,我在其中插入数字。而且,如果我在两列之一中插入一个数字,那么还有另外两列单元格会自动填充数学运算的结果。相反,如果我插入一个字母,datagridview 会显示一个错误。
问题是这样的:每当 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;
}
提前致谢!
【问题讨论】:
标签: c# winforms datagridview