You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中DataGridView加载SQLite数据时Decimal自动转整数的问题

解决DataGridView强制识别Decimal列的问题

问题描述

在VB.NET中从SQLite数据库查询数据并加载到DataGridView时,SQL查询返回的subtotal和price列是正确的Decimal值,但DataGridView会根据首条记录的类型自动转换列类型:

  • 首条记录为非零小数/0时,列显示正常
  • 首条记录为非零整数时,整列值会被四舍五入为整数

尝试设置.ValueType = GetType(Double)(加载前后均测试)无效,需要强制DataGridView始终将这两列识别为Decimal类型。

原实现代码:

Public Sub reload_SalesTicketItems(ByVal sql As String, ByVal DGV As Object)
    Try
        Strconnection.Open()
        Using strcon As SQLiteConnection = Strconnection()
            ds_SalesTicketItems = New DataSet

            Using cmd = New SQLiteCommand
                With cmd
                    .Connection = strcon
                    .CommandText = sql
                End With
                Using da As New SQLiteDataAdapter
                    da.SelectCommand = cmd
                    da.Fill(ds_SalesTicketItems, "Items")

                    With DGV
                        .Columns("subtotal").ValueType = GetType(Double)
                        .Columns("price").ValueType = GetType(Double)

                        .DataSource = ds_SalesTicketItems
                        .DataMember = "Items"
                    End With
                End Using
            End Using
        End Using
    Catch ex As Exception
        Debug.WriteLine("ERROR [34]: " & ex.Message)
    End Try
End Sub

解决方案

SQLite是动态类型数据库,DataAdapter默认会根据查询结果首行数据推断列类型,这是问题根源。需提前定义DataTable的列类型为Decimal,强制DataAdapter按指定类型填充数据。

修改后的代码:

Public Sub reload_SalesTicketItems(ByVal sql As String, ByVal DGV As DataGridView)
    Try
        ' 提前定义DataTable并指定列类型,需包含SQL查询返回的所有列
        Dim dtItems As New DataTable("Items")
        dtItems.Columns.Add("subtotal", GetType(Decimal))
        dtItems.Columns.Add("price", GetType(Decimal))
        ' 添加其他业务列示例,需与SQL返回列名一致
        ' dtItems.Columns.Add("id", GetType(Integer))
        ' dtItems.Columns.Add("product_name", GetType(String))

        Using strcon As SQLiteConnection = Strconnection()
            strcon.Open()
            Using cmd = New SQLiteCommand(sql, strcon)
                Using da As New SQLiteDataAdapter(cmd)
                    ' 填充预定义类型的DataTable,避免自动推断错误
                    da.Fill(dtItems)

                    With DGV
                        .DataSource = dtItems
                        ' 设置显示格式,确保小数位正常展示
                        .Columns("subtotal").DefaultCellStyle.Format = "N2"
                        .Columns("price").DefaultCellStyle.Format = "N2"
                    End With
                End Using
            End Using
        End Using
    Catch ex As Exception
        Debug.WriteLine("ERROR [34]: " & ex.Message)
    End Try
End Sub

关键优化点

  1. 预定义列类型:提前创建DataTable并指定subtotal和price为Decimal类型,从根源避免DataAdapter错误推断列类型。
  2. 连接管理优化:将连接打开操作移至Using块内部,确保连接正确释放。
  3. 类型安全:将DGV参数类型从Object改为DataGridView,提升代码可靠性。
  4. 显示格式设置:通过DefaultCellStyle.Format强制显示固定小数位,保证展示一致性。

内容的提问来源于stack exchange,提问作者CragMonkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:42:03