How to check if a number is bigger than in datagridview and add the value to next cell

nikos the 41 Reputation points

I have a datagrideview1 with some data in and i want to check for every row if a number in a specific cell is bigger than 1 for example and then add the value in an new column cell that i created.
How can i do that?

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,505 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,106 Reputation points

    First of all. Any way that does what is required without errors is 'a good way '. Although some ways to do itmay be more appropriate depending on circumstance.

    Since you asked about a condition where a value is >1, then when value is >2, I think you want to have a variable value used in the calculated result. If so, then here is some code that shows one of the ways (other ways are available at extra cost) that this can be done. It uses an extra column (which can be hidden) to hold the value to be used in condition. The results are dynamic, so when the TextBox is changed, the results are auto generated.
    All of this may be of no use to you, if so, just crumple it up and throw in the trash.


    ' Form1 with  
    ' DataGridView1  
    ' TextBox1  
    Option Strict On  
    Option Explicit On  
    Public Class Form1  
     Dim dt As New DataTable("freddy")  
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
     With dt  
     .Columns.Add("One", GetType(Integer))  
     .Columns.Add("Limit", GetType(Integer))  
     .Columns.Add("Result", GetType(Integer), "One > Limit")  
     Dim r As New Random  
     For i As Integer = 0 To 99  
     .Rows.Add(r.Next(-5, 5))  
     End With  
     DataGridView1.DataSource = dt  
     TextBox1.Text = "2"  
     ' this line will hide the Limit  
     ' column (uncomment to see)  
     ' DataGridView1.Columns("Limit").Visible = False  
     End Sub  
     Function GetLimit() As String  
     Dim v As Integer = 0  
     If Integer.TryParse(TextBox1.Text, v) Then Return TextBox1.Text  
     Return "9999999"  
     End Function  
     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged  
     Dim v As String = GetLimit()  
     For i As Integer = 0 To dt.Rows.Count - 1  
     dt(i)("Limit") = v  
     End Sub  
    End Class  

2 additional answers

Sort by: Most helpful
  1. nikos the 41 Reputation points

    I figured it out so this is what I did

     For i As Integer = DataGridView1.RowCount - 1 To 0 Step -1  
                    If CInt(Replace(DataGridView1.Rows(i).Cells(4).Value, ",", "")) > 2 Then  
    Me.DataGridView1.Rows(i).Cells(10).Value = 1  
    End If  

    Is this a good way to do that or I need to do something diferent?

    0 comments No comments

  2. nikos the 41 Reputation points

    I used your code, modified to match my needs and is working like a charm. Once again I appreciate your help. Thank you.

    0 comments No comments