在 DATAGRIDVIEW 比较中,输入字符串的格式不正确
input string was not in a correct format in DATAGRIDVIEW comparison
当我打开 window 我 select 数据到 DATAGRIDVIEW :
SELECT result_number as 'Result' ,
lowerlimit as 'Lower_Limit' ,
upperlimit as 'Upper_Limit'
FROM lab_results
在以下情况下,我需要更改 DATAGRIDVIEW 中的行颜色:
1-如果结果小于下限或结果大于上限。
2- 如果结果 = 阳性。
这是例子:
result Lower_Limit Upper_Limit
7.5 9 11
61.4 25 55
positive
在这 3 个结果行中,我需要将行颜色更改为红色我尝试了以下代码:
private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString()) < Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Lower_Limit"].Value.ToString()))
{
e.CellStyle.BackColor = Color.Red;
}
else if (Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Result"].Value) > Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Upper_Limit"].Value))
{
e.CellStyle.BackColor = Color.Red;
}
else if (dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString() == "positive")
{
e.CellStyle.BackColor = Color.Red;
}
else if (dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString() == "")
{
e.CellStyle.BackColor = Color.LightGray;
}
}
但是当打开表单时我得到错误输入字符串在第一行的格式不正确,请问如何解决这个错误?
如果结果列中的任何单元格不包含数字(例如单词“positive”或空字符串),则 Convert.ToDecimal 会引发异常。
但是您可以使用 decimal.TryParse 来避免异常并重新排列代码以在为超出范围的值应用颜色之前检查非数字字符串。
private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string value = dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString();
decimal.TryParse(value, out decimal result);
decimal.TryParse(dgvResult.Rows[e.RowIndex].Cells["Lower_Limit"].Value.ToString(), out decimal lower);
decimal.TryParse(dgvResult.Rows[e.RowIndex].Cells["Upper_Limit"].Value.ToString(), out decimal upper);
if (value == "")
{
e.CellStyle.BackColor = Color.LightGray;
}
else if (value == "positive" || result < lower || result > upper)
{
e.CellStyle.BackColor = Color.Red;
}
}
当我打开 window 我 select 数据到 DATAGRIDVIEW :
SELECT result_number as 'Result' ,
lowerlimit as 'Lower_Limit' ,
upperlimit as 'Upper_Limit'
FROM lab_results
在以下情况下,我需要更改 DATAGRIDVIEW 中的行颜色:
1-如果结果小于下限或结果大于上限。
2- 如果结果 = 阳性。
这是例子:
result Lower_Limit Upper_Limit
7.5 9 11
61.4 25 55
positive
在这 3 个结果行中,我需要将行颜色更改为红色我尝试了以下代码:
private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString()) < Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Lower_Limit"].Value.ToString()))
{
e.CellStyle.BackColor = Color.Red;
}
else if (Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Result"].Value) > Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells["Upper_Limit"].Value))
{
e.CellStyle.BackColor = Color.Red;
}
else if (dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString() == "positive")
{
e.CellStyle.BackColor = Color.Red;
}
else if (dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString() == "")
{
e.CellStyle.BackColor = Color.LightGray;
}
}
但是当打开表单时我得到错误输入字符串在第一行的格式不正确,请问如何解决这个错误?
如果结果列中的任何单元格不包含数字(例如单词“positive”或空字符串),则 Convert.ToDecimal 会引发异常。
但是您可以使用 decimal.TryParse 来避免异常并重新排列代码以在为超出范围的值应用颜色之前检查非数字字符串。
private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string value = dgvResult.Rows[e.RowIndex].Cells["Result"].Value.ToString();
decimal.TryParse(value, out decimal result);
decimal.TryParse(dgvResult.Rows[e.RowIndex].Cells["Lower_Limit"].Value.ToString(), out decimal lower);
decimal.TryParse(dgvResult.Rows[e.RowIndex].Cells["Upper_Limit"].Value.ToString(), out decimal upper);
if (value == "")
{
e.CellStyle.BackColor = Color.LightGray;
}
else if (value == "positive" || result < lower || result > upper)
{
e.CellStyle.BackColor = Color.Red;
}
}