Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How we can calculate the value of Datagridview cell like if cell value is 6+2+9 and result is shown in second column
Coulumn A Cloumn B
6+6+2 ???

What I have tried:

i tried this code but second cell not show result of first cell
Dim DocNo As String
If e.RowIndex >= 0 Then
Try
DocNo = Math.Abs(CDbl(DataGridView3.Rows(e.RowIndex).Cells(0).Value.ToString))
DataGridView3.Rows(e.RowIndex).Cells(1).Value = DocNo
Catch
DataGridView3.Rows(e.RowIndex).Cells(0).Value = "?"
End Try
Posted
Updated 13-Mar-22 21:38pm

1 solution

Your cell already contains a string: "6+6+2", so calling ToString on it doesn't do anything useful.

And strings that contain equations can't be "converted" to numbers by calling CDbl - all that will do is throw an exception - which you catch - because the whole string is not a number:
VB
Dim s As String = "6+6+2"
Dim DocNo As String
DocNo = Math.Abs(CDbl(s))
Console.WriteLine(DocNo)
Will throw an exception:
System.InvalidCastException: Conversion from string "6+6+2" to type 'Double' is not valid. ---> System.FormatException: Input string was not in a correct format.

What you need to do is evaluate the string expression as a number - this may help: An expression evaluator written in VB.NET[^]
Or you could use the DataTable.Compute[^] method:
Dim result = New DataTable().Compute("6+6+2", Nothing)
But that's not particularly efficient as you have to create a whole DataTable object to do it ...
 
Share this answer
 
Comments
Talha Muneer 14-Mar-22 4:02am    
if i enter the randomly value"12+65+6+3+4" in the datagridview cell 1 the sum of this equation show in the next cell of datagridview cell 2 which is 90 please help me on this issue
OriginalGriff 14-Mar-22 4:15am    
Read what I said ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900