Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
right now I have a vb.net program that reads in an excel file and displays the contents into a datagridview. My goal is to have code that tells the user if there is any value besides 1 or blank in the cell. Then I want to supply a default value of blank to the datagridviewcell. I was easily able to do all of this if the default value was 1 but when I change it to blank (String.empty) the program will keep the invalid input. Here is my code below. If anyone can figure out how to make the program supply a blank value as default value I would greatly appreciate it! :)

VB
Sub validateDGV(rowindex, columnindex)

    Dim value As String = DataGridView1.Rows(rowindex).Cells(columnindex).Value.ToString
    If (columnindex = 1) Then
        Dim cellData = DataGridView1.Rows(rowindex).Cells(columnindex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            'Do nothing because this is allowed
        ElseIf cellData <> 1 Then
            MessageBox.Show("Value must be 1 or Blank")
            DataGridView1.Rows(rowindex).Cells(columnindex).Value = String.Empty 'This should be supplying the default value of blank back to my datagridview but its not :(
            Exit Sub

        End If
    End If
End Sub
Posted
Comments
Maciej Los 19-Jul-13 15:19pm    
If your DGV is bind with MS Excel Sheet data (using OLEDB), then probably, default value of cell should be DBNull.Value.
CAS1224 20-Jul-13 10:55am    
Yes! It worked!!! Thanks so much. Please share this as the answer and I'll gladly accept
Maciej Los 21-Jul-13 13:08pm    
As you has suggested, i have added it as a solution.
Thank you ;)

1 solution

If your DGV is bind with MS Excel Sheet data (using OLEDB), then probably, default value of cell should be DBNull.Value.
 
Share this answer
 

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