Question DataGridView ignores Format

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
Hello.

Most (and I say most, 'cause sometimes it's working) of my DataGridViews are ignoring the given Format (within the Column.DefaultCellStyle). In my research I already stumpled upon the advice to set the ValueType too, but that doesn't help.

Background: I'm filling my DataGridViews per Hand from an ODBC.DataReader.toString. The formats were set within the 'Edit Clumns' dialog.

Does somebody know what I'm doing wrong?

Bobby
 
The formatting apply to data of various types and convert these to the given string representations. For example formatting a Date value to the short date pattern string, or formatting a Double value to a currency string. Data that already is a string can't be formatted, for example "2" is not an Integer, it is a String. Looking deeper you see that String.ToString method does not do any conversions, Integer.ToString can do that. If your data is stored as value types and not all strings you should be able to use GetValue or one of the other Get methods of the DataReader instead of ToString.
 
Hello John,

thanks for the quick response. I could have thought of that myself. *facepalm*. I always used
VB.NET:
DtRdr.Item(0).toString
'cause of an exception which was thrown when the Item was Nothing (seems like it wasn't the Item-Property at all).

The Item-Property does already return an Object, means the .toString conversion was the failure for me...and now my DataGridView is formatting the values the way I want.

Thank you very much,
Bobby
 
Back
Top