如何根据其他列输入自动更新datagridview中的列

时间:2017-02-08 04:55:17

标签: c#

我有一个datagridview dgvBlood,我有4列ResultRangeFromRangeToStatus。我只想根据Status中的输入Normal or AbnormalResultIf Result >= RangeFrom && Result <= RangeTo已经提供了Result = "Normal" else "Abnormal" RangeFrom and RangeTo。用户只需输入Result,我只想在用户输入后更新它。

这是我的示例代码。任何帮助都表示赞赏。

private void dgvBlood_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{   
    Double From = Convert.ToDouble(dgvBlood.CurrentRow.Cells["RangeFrom"].Value.ToString());
    Double To = Convert.ToDouble(dgvBlood.CurrentRow.Cells["RangeTo"].Value.ToString());
    Double Result = Convert.ToDouble(dgvBlood.CurrentRow.Cells["Result"].Value.ToString());

    String value = "Normal";
    String valueAb = "Abnormal";

    if (Result >= From && Result <= To)
    {
        dgvBlood.CurrentRow.Cells["Status"].Value = value;
    }
    else
    {
        dgvBlood.CurrentRow.Cells["Status"].Value = valueAb;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        Double From = Convert.ToDouble("0"+dataGridView1.CurrentRow.Cells["RangeFrom"].Value);
        Double To = Convert.ToDouble("0" + dataGridView1.CurrentRow.Cells["RangeTo"].Value);
        Double Result = Convert.ToDouble("0" + dataGridView1.CurrentRow.Cells["Result"].Value);

        if (Result >= From && Result <= To)
        {
            dataGridView1.CurrentRow.Cells["Status"].Value = "Normal";
        }
        else
        {
            dataGridView1.CurrentRow.Cells["Status"].Value = "Abnormal";
        }
    }

您必须设置一个条件来检查哪个列更改,状态应该更改。目前,它将检查任何单元格的变化。