私はデータベースからデータをロードするdatagridviewを持っています。そのdatagridviewには2つのintカラムがあり、そこに数字を挿入します。また、2つの列のいずれかに数値を挿入すると、数学的演算の結果を自動的に満たす別の2つの列セルがあります。逆に、文字を挿入すると、datagridviewにエラーが表示されます。DataGridviewエラーの後にCurrentCellDirtyStateChangedが機能しないC#
この問題は、datagridviewにエラーが表示されたときにエラーメッセージボックスの「OK」をクリックした後で、セルの編集を続けることができますが、CurrentCellDirtyStateChangedイベントは機能しません。したがって、他の2つの列のセルは自動的に塗りつぶされません。そして、私はエラーが表示された後に自動的に記入する必要があります。
これは私のコードです:
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;
}
感謝!