循环遍历GridView中的所有单元格并在小数上执行操作

时间:2017-04-12 14:48:07

标签: asp.net vb.net gridview foreach

我正在使用包含大量数据(字符串和小数)的GridView

我必须遍历所有单元格,如果单元格是小数,我必须在该小数上运行一个函数。

无法找到解决方案,任何想法?

3 个答案:

答案 0 :(得分:0)

确保选择正确的DGV。

/!\小心谨慎/!\

记住 Decimal.TryParse 适用于 CultureInfo.CurrentCulture.NumberFormat
因此,请务必使用或设置正确的文化。

'Check every row
For Each row As DataGridViewRow In DataGridView1.Rows
    'Check every cell
    For Each cell As DataGridViewCell In row.Cells
        Dim number As Decimal
            'Check if decimal
            If Decimal.TryParse(cell.Value.ToString, number) Then
                'Wohoo I am a decimal
            Else
                ' :( I Am not a decimal
            End If
    Next
Next

答案 1 :(得分:0)

要做到这一点,你需要2个cicles,我不测试代码,但会像这样:

For i = 0 To GridActive.rows.count - 1
For j = 0 To GridActive.Columns.count - 1
If GridActive.Item(i, j).value.contains(",") Then
Call DecimalFunction()
End If
Next
Next

答案 2 :(得分:0)

如果你需要在gridview bind时调用你的函数,那么你必须在rowdatabound事件中执行如下代码:(这是伪代码。)

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
 If e.Row.RowType = DataControlRowType.DataRow Then

  For Each TableCell  As DataGridViewCell In row.Cell
       If Decimal Condition = Yes Then ' Put your condition

        Else

        End If
  Next
 End If
End Sub