Datagridview Calculation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murali

    #1

    Datagridview Calculation

    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_C ellLeave(ByVal sender As Object, ByVal e As
    System.Windows. Forms.DataGridV iewCellEventArg s) Handles
    DataGridView1.C ellLeave
    If (TypeOf CType(sender,
    Windows.Forms.D ataGridView).Ed itingControl Is
    DataGridViewCom boBoxEditingCon trol) Then
    DataGridView1.E ditingPanel.Sel ect()
    End If
    If e.ColumnIndex = 1 Or e.ColumnIndex = 2 Then
    DataGridView1.R ows(e.RowIndex) .Cells(3).Value =
    CType(DataGridV iew1.Rows(e.Row Index).Cells(1) .Value *
    DataGridView1.R ows(e.RowIndex) .Cells(2).Value , Double)
    End If
    End Sub

    can anybody help me in this ...

    thanks

  • ClayB

    #2
    Re: Datagridview Calculation

    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(ByVa l sender As System.Object, ByVal e As
    EventArgs) Handles MyBase.Load
    DataGridView1.R owCount = 10
    DataGridView1.C olumnCount = 4

    End Sub

    Private Sub DataGridView1_C ellBeginEdit(By Val sender As Object,
    ByVal e As DataGridViewCel lCancelEventArg s) Handles
    DataGridView1.C ellBeginEdit
    If e.ColumnIndex = 3 Then
    e.Cancel = True
    End If
    End Sub

    Private Sub DataGridView1_C ellFormatting(B yVal sender As Object,
    ByVal e As DataGridViewCel lFormattingEven tArgs) Handles
    DataGridView1.C ellFormatting
    If e.ColumnIndex = 3 AndAlso e.RowIndex -1 Then
    e.Value = Convert.ToDoubl e(DataGridView1 (1,
    e.RowIndex).Val ue) * Convert.ToDoubl e(DataGridView1 (2,
    e.RowIndex).Val ue)
    End If
    End Sub


    Private Sub DataGridView1_C ellValidated(By Val sender As Object,
    ByVal e As DataGridViewCel lEventArgs) Handles
    DataGridView1.C ellValidated
    DataGridView1.R efresh()
    End Sub

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

    Comment

    Working...