Calculating Cells Value in DataGridView Using VB.Net

In this tutorial, I will teach you how to calculate the cells value in the datagridview using vb.net. This method will help you how to assign grades and points in the specific cells when entering the score in another cell. See the step by step below for your guide.

Creating Applicaion

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application. After that, do the form just like shown below. dtg1

Step 2

Double click the form and do the following code for adding data in the datagridview.
  1. With DataGridView1
  2. .Rows.Add("Math")
  3. .Rows.Add("English")
  4. .Rows.Add("Science")
  5. End With

Step 3

Go back to the design views and click the datagridview. In the properties, click events just like the lightning bolt and double click cellendedit event handler. dtg2

Step 4

Do the following code inside the method for calculating the score, grades and points.
  1. Try
  2. Dim colIndex As Integer = e.ColumnIndex
  3. If colIndex = 1 Then
  4. Dim DataGridView1cell As Integer = DataGridView1.CurrentRow.Cells(1).FormattedValue
  5. Select Case DataGridView1cell
  6. Case 0 To 24
  7. DataGridView1.CurrentRow.Cells(2).Value = "E"
  8. DataGridView1.CurrentRow.Cells(3).Value = "1"
  9.  
  10. Case 25 To 29
  11. DataGridView1.CurrentRow.Cells(2).Value = "D-"
  12. DataGridView1.CurrentRow.Cells(3).Value = "2"
  13.  
  14. Case 30 To 34
  15. DataGridView1.CurrentRow.Cells(2).Value = "D"
  16. DataGridView1.CurrentRow.Cells(3).Value = "3"
  17.  
  18. Case 35 To 39
  19. DataGridView1.CurrentRow.Cells(2).Value = "D+"
  20. DataGridView1.CurrentRow.Cells(3).Value = "4"
  21.  
  22. Case 40 To 44
  23. DataGridView1.CurrentRow.Cells(2).Value = "C-"
  24. DataGridView1.CurrentRow.Cells(3).Value = "5"
  25.  
  26. Case 45 To 54
  27. DataGridView1.CurrentRow.Cells(2).Value = "C"
  28. DataGridView1.CurrentRow.Cells(3).Value = "6"
  29.  
  30. Case 55 To 59
  31. DataGridView1.CurrentRow.Cells(2).Value = "C+"
  32. DataGridView1.CurrentRow.Cells(3).Value = "7"
  33.  
  34. Case 60 To 64
  35. DataGridView1.CurrentRow.Cells(2).Value = "B-"
  36. DataGridView1.CurrentRow.Cells(3).Value = "8"
  37.  
  38. Case 65 To 69
  39. DataGridView1.CurrentRow.Cells(2).Value = "B"
  40. DataGridView1.CurrentRow.Cells(3).Value = "9"
  41.  
  42. Case 70 To 74
  43. DataGridView1.CurrentRow.Cells(2).Value = "B+"
  44. DataGridView1.CurrentRow.Cells(3).Value = "10"
  45.  
  46. Case 75 To 79
  47. DataGridView1.CurrentRow.Cells(2).Value = "A-"
  48. DataGridView1.CurrentRow.Cells(3).Value = "11"
  49.  
  50. Case 80 To 100
  51. DataGridView1.CurrentRow.Cells(2).Value = "A"
  52. DataGridView1.CurrentRow.Cells(3).Value = "12"
  53.  
  54. Case Else
  55. DataGridView1.CurrentRow.Cells(2).Value = "0"
  56. DataGridView1.CurrentRow.Cells(3).Value = "0"
  57. End Select
  58. End If
  59. Catch ex As Exception
  60. MsgBox(ex.Message)
  61. End Try
The complete sourcecode is included. You can download it and run it on your computer. For more question about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT FB Account – https://www.facebook.com/onnaj.soicalap Or feel free to comment below.

Add new comment