2

If you set up a simple test form with a TextBox bound to a string property of a class and delete the content the value passed back to the property setter is "".

enter image description here

If you do the same thing with a cell in a DataGridView the value passed back to the property setter is Nothing.

enter image description here

Why the difference and is there anything we can apply to the DataGridView to make it behave the same as a TextBox?

hawbsl
  • 15,313
  • 25
  • 73
  • 114

3 Answers3

1

Nothing is VB's version of C#'s null or a database's NULL. It means that there is no value, as opposed to "" which means that there is a value, of an empty string.

I'm not entirely sure, but I suspect the difference is because a textbox's Text property is always a string, but a cell's Value property can be any object. You can use the null-coalescing operator to return an empty string when the value is Nothing.

Set(ByVal value As String)
   _Surname = If(value, "")
End Set

Alternatively, you can validate the cell post-edit and set it to ""explicitly. see here for an example - instead of messaging the user, you'd just change the value.

Community
  • 1
  • 1
Bobson
  • 13,498
  • 5
  • 55
  • 80
  • thanks, that makes sense, I'll try it. but it would also be nice if we didn't have to change hundreds of setters and could instead configure the DataGridView (or at least its DataGridViewTextBoxCell) to default to "" when a string is emptied out. Do you know if that's a config change we can make on the DataGridView? – hawbsl Dec 13 '12 at 09:14
  • @hawbsl - I don't, off hand. You can try making the change in the code that uses it, though. There's an [`IsNullOrEmpty()`](http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx) method you can use instead of checking whether it's equal to `""` – Bobson Dec 13 '12 at 14:18
  • reading your first paragraph again reminds we _exactly_ why we'd like the datagridview to treat the cell content as an empty string, because, *yes* there is a value there. the value is empty string. – hawbsl Dec 13 '12 at 14:48
  • @hawbsl - I edited in a link that might help with that. See if it's useful. – Bobson Dec 14 '12 at 15:03
1

if am correct the DataGridViewTextBoxCell by default is Null
and the TextBox by Default is String.Empty

you might check the Null vs Empty

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44
  • thanks, is there any way to setup the DataGridViewTextBoxCell with a default of String.Empty? – hawbsl Dec 13 '12 at 09:13
  • i can't say but can you give me a reason or what is your purpose this task? if you don't mind :) – spajce Dec 13 '12 at 14:09
  • at some point we have to write the class instance back to the database and if a stray string has been set to nothing the write will fail. so ... we can change the hundreds of setters to check for null, we can change many dozens of database writes to check for null, or ... much nicer ... we could just change a single DataGridView to treat a deleted cell as an empty string just as a TextBox does. that's my reason. – hawbsl Dec 13 '12 at 14:42
  • okay i got your point, so we have a extension to check the `DataGridViewTextBoxCell` either null or empty string. i assume that you already know the `For Loop` statment and try this code. `If String.IsNullOrWhiteSpace(DataGridView1.Rows(rowIndex).Cells(cellIndex).FormattedValue.ToString()) Then 'You logic here End If` i hope it will help :) – spajce Dec 13 '12 at 15:06
  • but anyway there's so many way to complete this task but for now the `For Loop` and `IsNullOrWhiteSpace` is i think the basic way solve your problem :) – spajce Dec 13 '12 at 15:30
0

DataGridView converts "" into Null during cell parsing.

If you handle CellParsing event and just set ParsingApplied, then no further parsing will be performed and the resulting value will remain "".

Eugene Ryabtsev
  • 2,232
  • 1
  • 23
  • 37