DataGridView and CheckboxColumn, instantly recompute?

seraphim04

Member
Joined
Oct 30, 2013
Messages
8
Programming Experience
1-3
screenshot.JPG
I am creating a program where sure will input the ff:
Amount - The basis for the computations
Discount Rate - if this is inputted the discount amount auto-compute based on Amount and Discount Rate
- this should auto-compute whenever there are changes in Amount or Discount Amount
Discount Amount - if this is inputted the discount amount auto-compute based on Amount and Discount Rate
- this should auto-compute whenever there are changes in Amount or Discount Rate
VAT INCLUSIVE - ​checkbox determines if vat is inclusive or not (Assistance required in this one).
VAT Amount - this should auto-compute whenever there is changed to Amount, Discount Amount or Discount Rate.
VAT RATE - VAT Amount is computed based in this, set to default 12.

Anyone can assist me on the checkbox, the problem with it is it should instantly recompute the VAT Amount upon clicking on it.
Right now, it will only recompute when the selection is removed from cell where the checkbox is.

Please help me with this.

Here are the codes that I currently have:
(The DataGridView is named as "gridarea"
VB.NET:
Private Sub gridarea_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridarea.CellEndEdit
        Dim temp As Double
        Dim i As Integer, j As Integer
        '----Format Number Values to Display with 2 Decimal Places----
        For i = 0 To gridarea.Rows.Count - 1
            For j = 0 To gridarea.Columns.Count - 1
                Select Case j
                    Case 1 To 3, 5 To 8
                        temp = gridarea.Rows(i).Cells(j).Value
                        gridarea.Rows(i).Cells(j).Value = Format(temp, "#0.00")
                End Select
            Next
        Next
        '----Computations----
        For i = 0 To gridarea.Rows.Count - 1
            If gridarea.CurrentCell.ColumnIndex = 1 Then '---Inputs Amount---
                '---Computes Discount Amount based on Amount and Discount Rate---
                gridarea.Rows(i).Cells(3).Value = Format(gridarea.Rows(i).Cells(1).Value * (gridarea.Rows(i).Cells(2).Value / 100), "#0.00")
            ElseIf gridarea.CurrentCell.ColumnIndex = 2 Then '---Inputs Discount Rate---
                '---Computes Discount Amount based on Amount and Discount Rate---
                gridarea.Rows(i).Cells(3).Value = Format(gridarea.Rows(i).Cells(1).Value * (gridarea.Rows(i).Cells(2).Value / 100), "#0.00")
            ElseIf gridarea.CurrentCell.ColumnIndex = 3 Then '---Inputs Discount Amount---
                '---Computes Discount Rate based on Amount and Discount Amount---
                gridarea.Rows(i).Cells(2).Value = Format((gridarea.Rows(i).Cells(3).Value / gridarea.Rows(i).Cells(1).Value) * 100, "#0.00")
            End If


            '---Computes VAT amount based on the VAT Rate Inputted---
            If gridarea.Rows(i).Cells(4).Value = False Then
                '---Computation if VAT is NOT INCLUSIVE---
                gridarea.Rows(i).Cells(5).Value = Format((gridarea.Rows(i).Cells(1).Value - gridarea.Rows(i).Cells(3).Value) * (Convert.ToDouble(txt_vatrate.Text) / 100), "#0.00")
            ElseIf gridarea.Rows(i).Cells(4).Value = True Then
                '---Computation if VAT IS INCLUSIVE---
                Dim discnet As Double = (gridarea.Rows(i).Cells(1).Value - gridarea.Rows(i).Cells(3).Value)
                gridarea.Rows(i).Cells(5).Value = Format((discnet - (discnet / (1 + (Convert.ToDouble(txt_vatrate.Text) / 100)))), "#0.00")
            End If
        Next
    End Sub
------------------
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        '----Add new row to the grid----
        gridarea.Rows.Add(gridarea.Rows.Count + 1, Format(0, "#0.00"), Format(0, "#0.00"), Format(0, "#0.00"), False, Format(0, "#0.00"))
    End Sub
------------------
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        gridarea.RowHeadersWidth = 30
        gridarea.AutoGenerateColumns = False
        Dim col00 As New DataGridViewTextBoxColumn
        col00.HeaderText = "No"
        col00.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        col00.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Dim col01 As New DataGridViewTextBoxColumn
        col01.HeaderText = "Amount"
        col01.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        col01.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Dim col02 As New DataGridViewTextBoxColumn
        col02.HeaderText = "Discount Rate"
        col02.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        col02.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Dim col03 As New DataGridViewTextBoxColumn
        col03.HeaderText = "Discount Amount"
        col03.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        col03.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Dim col04 As New DataGridViewCheckBoxColumn
        col04.HeaderText = "VAT Inclusive"
        col04.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        Dim col05 As New DataGridViewTextBoxColumn
        col05.HeaderText = "VAT Amount"
        col05.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        col05.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridarea.Columns.Add(col00)
        gridarea.Columns.Add(col01)
        gridarea.Columns.Add(col02)
        gridarea.Columns.Add(col03)
        gridarea.Columns.Add(col04)
        gridarea.Columns.Add(col05)
        gridarea.AllowUserToAddRows = False
        gridarea.Focus()
        gridarea.Columns(0).Width = 30
        gridarea.Columns(1).Width = 100
        gridarea.Columns(2).Width = 100
        gridarea.Columns(3).Width = 100
        gridarea.Columns(4).Width = 60
        gridarea.Columns(5).Width = 100
    End Sub
 
If you want to react as soon as the box is checked or unchecked then you need to handle the CellContentClick event of the grid. Make sure that you read the documentation because it addresses this situation specifically.
 
how do u delete a thread or a post?

...short answer - you don't.

By posting a question here and receiving comments and (hopefully) answers, you're helping the next guy who searches the site for your particular problem.

So, give yourself a pat on that back not only for joining a great community, but for helping improve it!
 
Back
Top