以下是为处理 vb.net 中的 datagridview 错误而创建的事件处理程序。我想确定哪个单元格引发了错误并根据该错误显示正确的错误消息。例如,如果用户没有在 datagridview 的特定单元格中输入正确格式的日期,我想显示一条消息,指出日期不在正确的格式。以下是gridview初始化代码和错误处理程序
Private Sub gridintialize()
Dim Itemcode As New DataGridViewColumn
Dim pkt As New DataGridViewColumn
Dim loose As New DataGridViewColumn
Dim desp As New DataGridViewColumn
Dim type As New DataGridViewColumn
Dim uom As New DataGridViewColumn
'Dim batch_no As New DataGridViewColumn
Dim expiry_date As New DataGridViewColumn
Dim qty As New DataGridViewColumn
Dim unit_price As New DataGridViewColumn
Dim disc As New DataGridViewColumn
Dim grossamt As New DataGridViewColumn
Dim disc_p As New DataGridViewColumn
Dim netamt As New DataGridViewColumn
With Itemcode
.Name = "Itemcode"
.ValueType = GetType(String)
.HeaderText = "ITEM CODE"
.Width = 195
.CellTemplate = New DataGridViewTextBoxCell
End With
With desp
.Name = "desc"
.ValueType = GetType(String)
.HeaderText = "Description"
.Width = 300
.CellTemplate = New DataGridViewTextBoxCell
End With
With type
.Name = "type"
.ValueType = GetType(String)
.HeaderText = "Type"
.Width = 50
.CellTemplate = New DataGridViewTextBoxCell
End With
With uom
.Name = "uom"
.ValueType = GetType(String)
.HeaderText = "UOM"
.Width = 60
.CellTemplate = New DataGridViewTextBoxCell
End With
With expiry_date
.Name = "expiry"
.ValueType = GetType(Date)
.HeaderText = "Expiry"
.Width = 70
.CellTemplate = New DataGridViewTextBoxCell
End With
With qty
.Name = "qty"
.ValueType = GetType(Integer)
.HeaderText = "Qty"
.Width = 70
.CellTemplate = New DataGridViewTextBoxCell
End With
With unit_price
.Name = "unitp"
.ValueType = GetType(Double)
.HeaderText = "Unit Price"
.Width = 70
.CellTemplate = New DataGridViewTextBoxCell
End With
With disc
.Name = "disc"
.ValueType = GetType(Double)
.HeaderText = "Discount"
.Width = 70
.CellTemplate = New DataGridViewTextBoxCell
End With
With disc_p
.Name = "discp"
.ValueType = GetType(Double)
.HeaderText = "Disc(%)"
.Width = 70
.CellTemplate = New DataGridViewTextBoxCell
End With
With grossamt
.Name = "gamt"
.ValueType = GetType(Double)
.HeaderText = "Gross Amount"
.Width = 70
.ReadOnly = True
.CellTemplate = New DataGridViewTextBoxCell
End With
With netamt
.Name = "namt"
.ValueType = GetType(Double)
.HeaderText = "Net Amount"
.Width = 80
.ReadOnly = True
.CellTemplate = New DataGridViewTextBoxCell
End With
With pkt
.Name = "pkt"
.HeaderText = "PKT"
.Width = 45
.CellTemplate = New DataGridViewTextBoxCell
End With
With loose
.Name = "loose"
.HeaderText = "Loose"
.Width = 45
.CellTemplate = New DataGridViewTextBoxCell
End With
With dgsalesitem
.Columns.Add(pkt)
.Columns.Add(loose)
.Columns.Add(Itemcode)
.Columns.Add(desp)
.Columns.Add(type)
.Columns.Add(uom)
.Columns.Add(expiry_date)
.Columns.Add(qty)
.Columns.Add(unit_price)
.Columns.Add(grossamt)
.Columns.Add(disc_p)
.Columns.Add(disc)
.Columns.Add(netamt)
End With
End Sub
Private Sub dgsalesitem_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgsalesitem.DataError
MsgBox("Please enter data in correct format", vbInformation, "POS SYSTEM")
End Sub