如果将DataGridView中的单元格值分配给VbNull对于特定列,它显示为1 [英] If Assigning Cell Values in DataGridView to VbNull For a particular column it's showing 1

查看:70
本文介绍了如果将DataGridView中的单元格值分配给VbNull对于特定列,它显示为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VB的新手...用于学习目的我正在尝试编辑DataGridView中的Cell值...如果单元格值为空或者我尝试显示NULL ...如果我正在使用此代码



I Am New to VB... For Learning Purpose am trying to Edit the Cell values in DataGridView... If cell values is nothing or empty am trying to show NULL... If am Using this code

Private Sub Btn_ShowAll_Click(sender As System.Object, e As System.EventArgs) Handles Btn_ShowAll.Click
       Dim da As New SqlDataAdapter("Select * From Tablexy", con)
       Dim ds As New DataTable("Tablexy")
       Dim dgr As DataGridViewRow
       con.Open()
       da.Fill(ds)
       Dgv1.DataSource = ds
       con.Close()
       For r As Int32 = 0 To Dgv1.Rows.Count - 2
           dgr = Dgv1.Rows(r)
           For i As Int32 = 0 To dgr.Cells.Count - 2
               If Convert.ToString(dgr.Cells(i).Value) = "" Then
                   dgr.Cells(i).Value = vbNull
               End If
           Next
       Next

   End Sub





但是对于一列订购费用值显示为1,即使它们是空值...



Tablexy中的列

产品(Pk,Nvarchar(50)NotNull ),ProductDesc(Nvarchar(50),Null),ProductType(Nvarchar(50),Null),Holdingcost(Real,NotNull),OrderingCost(Real,Null)。



为什么OrderingCost显示为1 ....任何解释都会有帮助....



But For One Column OrderingCost The Values are showing as 1 even though they are null values...

Columns in Tablexy
Product (Pk,Nvarchar(50) NotNull),ProductDesc(Nvarchar(50),Null), ProductType(Nvarchar(50),Null),Holdingcost (Real, NotNull),OrderingCost(Real,Null).

Why is OrderingCost shown as 1.... Any Explanation Will be Helpful....

推荐答案

实际上你只是在分配值时犯了一个错误。
You are actually just doing a mistake in assigning the value.
vbNull

是VB中的公共常量,它有一个值为1的变体类型,因此是1值。



正确的方法是

is a public constant in VB and it has a variant type with value of 1, thus the 1 value.

The correct way to do it is

dgr.Cells(i).Value = DBNull.Value


这篇关于如果将DataGridView中的单元格值分配给VbNull对于特定列,它显示为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆