Datagridview Calculation

M

Murali

dear all,
I am very new to vb.net 2. This is my post to this site. I have some
doubts in Datagridview.
I am using datagridview in unbound mode. in datagridview i have 4
coloumns

product qty price amount

product coloumn is a combo box and others textbox. now when i enter
leave qty or price i want the amount to be calculated and displayed
automatically. below is the code which i used

but when leaving the price coloumn the amount is not calculating. if i
back to qty and pressing tab again it is calculating

code..

Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellLeave
If (TypeOf CType(sender,
Windows.Forms.DataGridView).EditingControl Is
DataGridViewComboBoxEditingControl) Then
DataGridView1.EditingPanel.Select()
End If
If e.ColumnIndex = 1 Or e.ColumnIndex = 2 Then
DataGridView1.Rows(e.RowIndex).Cells(3).Value =
CType(DataGridView1.Rows(e.RowIndex).Cells(1).Value *
DataGridView1.Rows(e.RowIndex).Cells(2).Value, Double)
End If
End Sub

can anybody help me in this ...

thanks
 
C

ClayB

Instead of trying to change the 3rd value when any of the 2 other
values change, you could just try to compute the 3rd anytime it is
needed by the DataGridView. Handling these events tries to do this.


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
EventArgs) Handles MyBase.Load
DataGridView1.RowCount = 10
DataGridView1.ColumnCount = 4

End Sub

Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object,
ByVal e As DataGridViewCellCancelEventArgs) Handles
DataGridView1.CellBeginEdit
If e.ColumnIndex = 3 Then
e.Cancel = True
End If
End Sub

Private Sub DataGridView1_CellFormatting(ByVal sender As Object,
ByVal e As DataGridViewCellFormattingEventArgs) Handles
DataGridView1.CellFormatting
If e.ColumnIndex = 3 AndAlso e.RowIndex > -1 Then
e.Value = Convert.ToDouble(DataGridView1(1,
e.RowIndex).Value) * Convert.ToDouble(DataGridView1(2,
e.RowIndex).Value)
End If
End Sub


Private Sub DataGridView1_CellValidated(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) Handles
DataGridView1.CellValidated
DataGridView1.Refresh()
End Sub

=====================
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top