Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

I want to do multiplication in cell change event of cell(2) and Cell(3) and want to show in cell(4) i got this error when i run this program.."Value Cannot be Null"

this is the table
|Sno| ProductName  |Quantity|PerPrice |  Total         |
======================================================
|1  | Coke 200ml   |20      |20       |Here is Error   |

this is the code..
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As                  System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter

 If DataGridView1.CurrentCell.ColumnIndex = 4 Then
            Dim n As Integer = DataGridView1.RowCount - 1

DataGridView1.Rows(n).Cells(4).Value = (Double.Parse(DataGridView1.Rows(n).Cells(2).Value) * Double.Parse(DataGridView1.Rows(n).Cells(3).Value)).ToString


        End If
    End Sub
Posted
Updated 13-Apr-14 22:36pm
v4
Comments
Richard MacCutchan 14-Apr-14 4:26am    
Don't try and do complex operations in a single line. Break your statement into its constituent parts and check the results at each stage. If either of your text boxes contain illegal characters then Double.Parse will fail. Use the TryParse method and check that it succeeds.

VB
Private Sub DataGridView1_CellEnter(...) Handles DataGridView1.CellEnter
    If DataGridView1.CurrentCell.ColumnIndex = 4 Then
        Dim n As Integer = DataGridView1.RowCount - 1
        Dim Qty As Double = 0
        Double.TryParse(DataGridView1.Rows(n).Cells(2).Value, out Qty)
        Dim PerPrice As Double = 0
        Double.TryParse(DataGridView1.Rows(n).Cells(3).Value, out PerPrice)
        DataGridView1.Rows(n).Cells(4).Value = ( Qty * PerPrice).ToString()
    End If
End Sub

you can use CurrentCellDirtyStateChanged Event for count total when editing qty/per-price cells
visit link...
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx[^]
Happy coding!
:)
 
Share this answer
 
v2
Comments
Naveen Roy 15-Apr-14 4:49am    
Thanks for suggestion but when i using above code after provide values its ans showing 0 in the below cell even in the next row its again showing 0 when i using tab in the below cell without giving any numbers.
just like this

|Sno| ProductName |Quantity|PerPrice | Total |
======================================================
|1 | Coke 200ml |20 |20 | |
| | | | | 0 |
| | | | | 0 |
Aarti Meswania 15-Apr-14 4:55am    
take a look to your code block

Dim n As Integer = DataGridView1.RowCount - 1

it is causing issue what you are doing everytime calculating "total" for grid's last line
DataGridView1.RowCount - 1

you should use CurrentCellDirtyStateChanged event and e.rowindex/columnindex property to get currrent row/col
follow link I suggested in my answer
Naveen Roy 17-Apr-14 3:10am    
thank you, your above code is working.just changed some code and now is working fine thanks a lot..
Aarti Meswania 17-Apr-14 4:30am    
Most welcome!
Glad to help you! :)
Chk this :
VB
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As                  System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
 
 If DataGridView1.CurrentCell.ColumnIndex = 4 Then
            Dim n As Integer = DataGridView1.RowCount - 1
dim  first_value as String
dim  Second_value as String
If Not IsDBNull(DataGridView1.Rows(n).Cells(2).Value) AndAlso DataGridView1.Rows(n).Cells(2).Value.ToString.Length <> 0 Then
 first_value =DataGridView1.Rows(n).Cells(2).Value
else
first_value ="0"
end

If Not IsDBNull(DataGridView1.Rows(n).Cells(3).Value) AndAlso DataGridView1.Rows(n).Cells(2).Value.ToString.Length <> 0 Then
 Second_value =DataGridView1.Rows(n).Cells(3).Value
else
Second_value ="0"
end


DataGridView1.Rows(n).Cells(4).Value = (Double.Parse(First_value ) * Double.Parse(Second_value )).ToString
  End If
    End Sub
 
Share this answer
 
Comments
Naveen Roy 17-Apr-14 4:38am    
thanks for help sir...

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