Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


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....
Posted
Updated 4-Jun-13 0:43am
v3

1 solution

You are actually just doing a mistake in assigning the value.
VB
vbNull
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
VB
dgr.Cells(i).Value = DBNull.Value
 
Share this answer
 
Comments
Raja Sekhar S 12-Jun-13 0:55am    
Thank You For the Answer Ezra Neil... +5 For the Solution...
Raja Sekhar S 13-Jun-13 5:21am    
@Ezra Neil: What if i Want to Show 0 if the Data is Null for Integer/Real Data Type and "Null" if Datatype is Nvarchar....?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900