Click here to Skip to main content
16,013,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a vb.net windows form application using a datagridview. I'm hoping to find a way to prevent a user from entering whitespaces or empty strings and if they enter invalid input. I'll have a message appear explaining their mistake and thereafter a default value will be supplied. This is what I have so far and it prevents completely blank cells but if I had a whitespace (i.e. press space bar to add blank string) it doesn't know that it's still blank input.

VB
If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString Is Nothing OrElse IsDBNull(cellString) OrElse cellString.ToString = String.Empty Then

                MessageBox.Show("Cannot Be Empty")
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If
Posted

1 solution

... you could try removing the whitespace and checking the length.

Something like the below should be a good start.

VB
If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString Is Nothing OrElse IsDBNull(cellString) OrElse cellString.ToString = String.Empty OrElse cellString.Trim().Length=0 Then
 
                MessageBox.Show("Cannot Be Empty")
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If
 
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