VB.net 单元格值 Return DBNull
VB.net Cell Value Return DBNull
我有一个带有自定义 DataTable 的 DataGridView(不是 SQL 数据库)。我的编码是这样的
Sub Tabel()
Tabel1 = New DataTable
With Tabel1
.Columns.Add("kodebarang")
.Columns.Add("namabarang")
.Columns.Add("satuan")
.Columns.Add("harga", GetType(Double))
.Columns.Add("jumlah", GetType(Integer))
.Columns.Add("hargatotal", GetType(Double))
End With
DataGridView1.DataSource = Tabel1
With DataGridView1
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Item Name"
.Columns(2).HeaderText = "Unit"
.Columns(3).HeaderText = "Unit Price"
.Columns(4).HeaderText = "Qty"
.Columns(5).HeaderText = "Total Price"
.Columns(3).DefaultCellStyle.Format = "###,###,###"
.Columns(5).DefaultCellStyle.Format = "###,###,###"
.Columns(1).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(3).ReadOnly = True
.Columns(5).ReadOnly = True
.Columns(3).DefaultCellStyle.BackColor = Color.LightBlue
.Columns(4).DefaultCellStyle.BackColor = Color.LightGray
.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(1).HeaderCell.Style.BackColor = Color.LightBlue
.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next i
End Sub
我使用 CellEndEdit 更改数据网格单元格值。
Private Sub DataGridView_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
celWasEndEdit = DataGridView1(e.ColumnIndex, e.RowIndex)
txtNamaBarang.Text = DataGridView1.Rows.Count - 1
If e.ColumnIndex = 0 Then
Call Connect()
Sql = "SELECT * FROM databarang WHERE kodebarang='" & DataGridView1.CurrentRow.Cells(0).Value & "'"
Cmd = New OdbcCommand(Sql, Con)
Read = Cmd.ExecuteReader
While Read.Read()
DataGridView1.Rows(e.RowIndex).Cells(1).Value = Read("namabarang")
DataGridView1.Rows(e.RowIndex).Cells(2).Value = Read("satuan")
DataGridView1.Rows(e.RowIndex).Cells(3).Value = Read("hargapartai")
End While
ElseIf e.ColumnIndex = 4 Then
Dim Quantity As Integer = DataGridView1.Rows(e.RowIndex).Cells(4).Value
Dim UnitPrice As Integer = DataGridView1.Rows(e.RowIndex).Cells(3).Value
Dim TotalPrice As Integer = Quantity * UnitPrice
DataGridView1.Rows(e.RowIndex).Cells(5).Value = TotalPrice
End If
End Sub
DataGridView Screenshot
但是当我更改第 4 列(数量)值时出现错误。 从类型 'DBNull' 到类型 'Integer' 的转换无效。,但如果将我的代码更改为
ElseIf e.ColumnIndex = 4 Then
DataGridView1.Rows(e.RowIndex).Cells(3).Value, MsgBoxStyle.Information, "Error")
End If
我可以在 MsgBox 中获取第 3 列的值。
如果您的 DataGridView 绑定到 DataTable,则无需执行任何操作;只需在计算其他两列的倍数的总计列上设置一个表达式:
Tabel1 = New DataTable
With Tabel1
.Columns.Add("kodebarang")
.Columns.Add("namabarang")
.Columns.Add("satuan")
.Columns.Add("harga", GetType(Double))
.Columns.Add("jumlah", GetType(Integer))
Dim dc = .Columns.Add("hargatotal", GetType(Double)) ''changed line
dc.Expression = "[harga] * [jumlah]" ''new line
End With
DataGridView1.DataSource = Tabel1
在相关说明中,您不应该通过 datagridview 访问数据;数据存在于数据表中,应该从那里访问,而不是通过显示它的控件
我有一个带有自定义 DataTable 的 DataGridView(不是 SQL 数据库)。我的编码是这样的
Sub Tabel()
Tabel1 = New DataTable
With Tabel1
.Columns.Add("kodebarang")
.Columns.Add("namabarang")
.Columns.Add("satuan")
.Columns.Add("harga", GetType(Double))
.Columns.Add("jumlah", GetType(Integer))
.Columns.Add("hargatotal", GetType(Double))
End With
DataGridView1.DataSource = Tabel1
With DataGridView1
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Item Name"
.Columns(2).HeaderText = "Unit"
.Columns(3).HeaderText = "Unit Price"
.Columns(4).HeaderText = "Qty"
.Columns(5).HeaderText = "Total Price"
.Columns(3).DefaultCellStyle.Format = "###,###,###"
.Columns(5).DefaultCellStyle.Format = "###,###,###"
.Columns(1).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(3).ReadOnly = True
.Columns(5).ReadOnly = True
.Columns(3).DefaultCellStyle.BackColor = Color.LightBlue
.Columns(4).DefaultCellStyle.BackColor = Color.LightGray
.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(1).HeaderCell.Style.BackColor = Color.LightBlue
.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next i
End Sub
我使用 CellEndEdit 更改数据网格单元格值。
Private Sub DataGridView_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
celWasEndEdit = DataGridView1(e.ColumnIndex, e.RowIndex)
txtNamaBarang.Text = DataGridView1.Rows.Count - 1
If e.ColumnIndex = 0 Then
Call Connect()
Sql = "SELECT * FROM databarang WHERE kodebarang='" & DataGridView1.CurrentRow.Cells(0).Value & "'"
Cmd = New OdbcCommand(Sql, Con)
Read = Cmd.ExecuteReader
While Read.Read()
DataGridView1.Rows(e.RowIndex).Cells(1).Value = Read("namabarang")
DataGridView1.Rows(e.RowIndex).Cells(2).Value = Read("satuan")
DataGridView1.Rows(e.RowIndex).Cells(3).Value = Read("hargapartai")
End While
ElseIf e.ColumnIndex = 4 Then
Dim Quantity As Integer = DataGridView1.Rows(e.RowIndex).Cells(4).Value
Dim UnitPrice As Integer = DataGridView1.Rows(e.RowIndex).Cells(3).Value
Dim TotalPrice As Integer = Quantity * UnitPrice
DataGridView1.Rows(e.RowIndex).Cells(5).Value = TotalPrice
End If
End Sub
DataGridView Screenshot
但是当我更改第 4 列(数量)值时出现错误。 从类型 'DBNull' 到类型 'Integer' 的转换无效。,但如果将我的代码更改为
ElseIf e.ColumnIndex = 4 Then
DataGridView1.Rows(e.RowIndex).Cells(3).Value, MsgBoxStyle.Information, "Error")
End If
我可以在 MsgBox 中获取第 3 列的值。
如果您的 DataGridView 绑定到 DataTable,则无需执行任何操作;只需在计算其他两列的倍数的总计列上设置一个表达式:
Tabel1 = New DataTable
With Tabel1
.Columns.Add("kodebarang")
.Columns.Add("namabarang")
.Columns.Add("satuan")
.Columns.Add("harga", GetType(Double))
.Columns.Add("jumlah", GetType(Integer))
Dim dc = .Columns.Add("hargatotal", GetType(Double)) ''changed line
dc.Expression = "[harga] * [jumlah]" ''new line
End With
DataGridView1.DataSource = Tabel1
在相关说明中,您不应该通过 datagridview 访问数据;数据存在于数据表中,应该从那里访问,而不是通过显示它的控件