如何比较datagridview单元格的十进制类型的值

时间:2013-09-06 07:30:06

标签: c# datagridview

我制作了这段代码

(DataGridViewRow  dgRow in dgvMarksEntryForClass.Rows)
{
     if (dgRow.Cells["dgcolMarksObtain"].Value.GetType is decimal)
     {
         //some action
     }
     else { 
         //some action
     }
}

如何检查

的值是否为
dgRow.Cells["dgcolMarksObtain"].Value.GetType

是十进制类型吗?

3 个答案:

答案 0 :(得分:2)

您可以使用十进制TryParse()方法。

(DataGridViewRow  dgRow in dgvMarksEntryForClass.Rows)
{
    Decimal cellValue;
    if (Decimal.TryParse(dgRow.Cells["dgcolMarksObtain"].Value, out cellValue)
    {
         //some action
    }
    else
    {
        //some action
    }
}

答案 1 :(得分:0)

decimal value;
if(Decimal.TryParse(dgRow.Cells["dgcolMarksObtain"].Value.ToString(), out value))
  // It's a decimal
else
  // No it's not.

答案 2 :(得分:-2)

试试这个 -

(DataGridViewRow  dgRow in dgvMarksEntryForClass.Rows)
{
     if (dgRow.Cells["dgcolMarksObtain"].Value.GetType() is decimal)
     {
         //some action
     }
     else { 
         //some action
     }
}
相关问题