C# 将datagrid项检查为数字项
我试图得到一个datagrid,根据物品的数量显示不同颜色的行 我遇到的问题是if语句:C# 将datagrid项检查为数字项,c#,windows,forms,types,row,C#,Windows,Forms,Types,Row,我试图得到一个datagrid,根据物品的数量显示不同颜色的行 我遇到的问题是if语句: if (RowType == < 5) if(行类型==5) { row.DefaultCellStyle.BackColor=颜色.白色; row.DefaultCellStyle.ForeColor=Color.Black; } else if(行类型==/
if (RowType == < 5)
if(行类型==<5)
无效,如何根据值解析要检查的行类型
foreach (DataGridViewRow row in dtaPart.Rows)
{
//Cell three is where quantity is
string RowType = row.Cells[3].Value.ToString();
if (RowType == //> 5)
{
row.DefaultCellStyle.BackColor = Color.White;
row.DefaultCellStyle.ForeColor = Color.Black;
}
else if (RowType == //< 5)
{
row.DefaultCellStyle.BackColor = Color.Orange;
row.DefaultCellStyle.ForeColor = Color.Black;
}
else if (RowType == //< 1)
{
row.DefaultCellStyle.BackColor = Color.Red;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
foreach(dtaPart.Rows中的DataGridViewRow行)
{
//第三单元是数量的所在
字符串RowType=row.Cells[3].Value.ToString();
如果(行类型==/>5)
{
row.DefaultCellStyle.BackColor=颜色.白色;
row.DefaultCellStyle.ForeColor=Color.Black;
}
else if(行类型==/<5)
{
row.DefaultCellStyle.BackColor=Color.Orange;
row.DefaultCellStyle.ForeColor=Color.Black;
}
else if(行类型==/<1)
{
row.DefaultCellStyle.BackColor=Color.Red;
row.DefaultCellStyle.ForeColor=Color.Black;
}
}
您可以使用double value=double.Parse(RowType)
然后检查value>5、<5、<1
等
但是,如果datagrid列已经包含数值类型,如double
或int
,则不需要调用ToString()
,然后解析字符串;因此,与此相反:
string RowTypeStr = row.Cells[3].Value.ToString();
double RowType = double.Parse(RowTypeStr);
但您可以直接投射值:
double RowType = (double)row.Cells[3].Value;