Dear Sir,
I'm Trying to make empty datagridview column/cell directly values to zero(0) in VB.NET.
so I want when I make an empty column or cell in the DatagridView it immediately goes to zero (0).
Please Guide me
Thanks
Public Class Form2
Public _myTable As New DataTable()
Public _dr As DataRow
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
_myTable.Columns.AddRange({
New DataColumn("Code", GetType(String)),
New DataColumn("Quantity", GetType(Integer)),
New DataColumn("Price", GetType(Integer)),
New DataColumn("Discount", GetType(Integer)),
New DataColumn("Total", GetType(Integer))})
Me.CenterToScreen()
End Sub
Private Sub CalculateTotalqty()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim quantity As Double
If Not IsDBNull(item.Cells("Quantity").Value) Then
Integer.TryParse(CType(item.Cells("Quantity").Value, String), CInt(quantity))
End If
tot += quantity
Next item
lblQty.Text = tot.ToString("N2")
End Sub
Private Sub CalculateTotalValue()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim quantity As Double = 0
Dim price As Double = 0
Dim discount As Double = 0
If Not IsDBNull(item.Cells("Quantity").Value) Then
Integer.TryParse(CType(item.Cells("Quantity").Value, String), CInt(quantity))
End If
If Not IsDBNull(item.Cells("Price").Value) Then
Integer.TryParse(CType(item.Cells("Price").Value, String), CInt(price))
End If
If Not IsDBNull(item.Cells("Discount").Value) Then
Integer.TryParse(CType(item.Cells("Discount").Value, String), CInt(discount))
End If
tot += (quantity * price) - (discount)
Next item
lblValue.Text = tot.ToString("N")
End Sub
Private Sub BtnAddproduct_Click(sender As Object, e As EventArgs) Handles BtnAddproduct.Click
_myTable.Rows.Add("A", 2, 10000, 0, 20000)
'DataGridView1.AutoGenerateColumns = False
' Disable adding new row
DataGridView1.AllowUserToAddRows = False
' Create our medium between grid and collection
DataGridView1.DataSource = _myTable
CalculateTotalqty()
CalculateTotalValue()
End Sub
Private Sub datagridview1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
CalculateTotalqty()
CalculateTotalValue()
Dim price As Integer = Nothing, quantity As Integer = Nothing, Discount As Integer = Nothing, total As Integer = Nothing
If DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value IsNot Nothing Then
If Not Integer.TryParse(DataGridView1.Rows(e.RowIndex).Cells("Quantity").Value.ToString(), quantity) Then
quantity = 0
End If
Else
quantity = 0
End If
If DataGridView1.Rows(e.RowIndex).Cells("Price").Value IsNot Nothing Then
If Not Integer.TryParse(DataGridView1.Rows(e.RowIndex).Cells("Price").Value.ToString(), price) Then
price = 0
End If
Else
price = 0
End If
If DataGridView1.Rows(e.RowIndex).Cells("Discount").Value IsNot Nothing Then
If Not Integer.TryParse(DataGridView1.Rows(e.RowIndex).Cells("Discount").Value.ToString(), Discount) Then
Discount = 0
End If
Else
Discount = 0
End If
total = (quantity * price) - (quantity * Discount)
DataGridView1.Rows(e.RowIndex).Cells("Total").Value = total
End Sub
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
If e.Context = DataGridViewDataErrorContexts.Commit Then
'MessageBox.Show(String.Format("Column {0} value is required.", e.ColumnIndex))
End If
End Sub
Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
_dr = _myTable.Rows(e.RowIndex)
End Sub
End Sub