-
Jul 20th, 2022, 10:42 PM
#1
Thread Starter
New Member
What is the value when I delete the string in a DGV cell?
Good day guys,
I'm encountering a problem with Datagridview when I delete or erase the value in a cell.
Here's my code
Code:
Private Sub DailyView_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DailyView.CellEndEdit
DailyView.CurrentCell.Value = Trim(UCase(DailyView.CurrentCell.Value))
If DailyView.CurrentCell.Value = "VL" Or DailyView.CurrentCell.Value = "SL" Or DailyView.CurrentCell.Value Is Nothing Or DailyView.CurrentCell.Value = "" Then
MsgBox("ok")
Else
DailyView.CurrentCell.Value = ""
MsgBox("Value input is not allowed." & vbCrLf & "Value deleted")
End If
End Sub
The scenario is when I insert a correct record like VL, then I change my mind or decided to erase it using backspace or del button, my programs always jump on the "else" which is value is not allowed. Before this I also try
Code:
DailyView.CurrentCell.Value = Trim(UCase(DailyView.CurrentCell.Value))
If DailyView.CurrentCell.Value <> "VL" AND DailyView.CurrentCell.Value <> "SL" AND DailyView.CurrentCell.Value Isnot Nothing AND DailyView.CurrentCell.Value <> "" Then
MsgBox("Value input is not allowed." & vbCrLf & "Value deleted")
End If
But still I get the same result. So my question is, do deleted string on a cell have a value or something?
Advance Thanks for the help.
-
Jul 21st, 2022, 06:00 AM
#2
Re: What is the value when I delete the string in a DGV cell?
When you set a breakpoint on the If statement and actually looked at the value of DailyView.CurrentCell.Value, what did it tell you?
By the way, you should not be using CurrentCell in cell-based event handlers. The e parameter provides the ColumnIndex and RowIndex of the cell the event is for so you should use those values to get the corresponding cell.
Also, don't use the same complex expression over and over. In that first snippet you're using DailyView.CurrentCell.Value six times. That's bad. Use it once and assign the result to a variable, then use that variable repeatedly.
Finally, the fact that you are comparing the Value property of a cell to a String literal demonstrates that you have Option Strict Off. That is, unfortunately, the default but you should remedy that immediately, setting Option Strict On in the project properties and also in the VS options, so it will be On by default in all future projects. That will force you to put more thought into your data types, which will make you write better code. If you want to compare the cell context to a String, you need to check whether it actually is a String and then cast as that type.
-
Jul 21st, 2022, 04:53 PM
#3
Re: What is the value when I delete the string in a DGV cell?
The default cell value is Nothing, but…, that changes if you enter a string, then delete the string. At that point the cell value is “”
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 21st, 2022, 07:23 PM
#4
Thread Starter
New Member
Re: What is the value when I delete the string in a DGV cell?
Originally Posted by .paul.
The default cell value is Nothing, but…, that changes if you enter a string, then delete the string. At that point the cell value is “”
Hi Paul,
I also compare it to value "" but still it doesn't work.
-
Jul 21st, 2022, 07:28 PM
#5
Thread Starter
New Member
Re: What is the value when I delete the string in a DGV cell?
When I erase the value and the if statement triggers, I always get the MsgBox("Value input is not allowed." & vbCrLf & "Value deleted").
I'm planning to get also the value of the cell, but I want to check it first if the value is valid, so I can lessen the loop when saving to database.
-
Jul 21st, 2022, 07:39 PM
#6
Re: What is the value when I delete the string in a DGV cell?
Code:
If (DailyView.CurrentCell.Value <> "VL" OR DailyView.CurrentCell.Value <> "SL") Then
MsgBox("Value input is not allowed." & vbCrLf & "Value deleted")
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 21st, 2022, 08:43 PM
#7
Re: What is the value when I delete the string in a DGV cell?
Originally Posted by .paul.
Code:
If (DailyView.CurrentCell.Value <> "VL" OR DailyView.CurrentCell.Value <> "SL") Then
MsgBox("Value input is not allowed." & vbCrLf & "Value deleted")
End If
That's not right. That would match every value. If you look at the original code, it was using 'Or' with '=' and 'And' with '<>', which appears to be correct.
-
Jul 21st, 2022, 08:44 PM
#8
Re: What is the value when I delete the string in a DGV cell?
Originally Posted by Pendot
Hi Paul,
I also compare it to value "" but still it doesn't work.
So actually look to see what the actual value is. VS doesn't include a debugger for no reason. Use it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|