Format string in DataGrid

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

VB.Net 2005

I have a non-bound datagridview that I fill with a column that contains a
UPC number (as a string).

I want the display format to be like this: 0 12345 67890 1.

I have tried setting the format at design time to "# ##### ##### #" and also
in the cell formatting event with:
e.Value = Format(e.Value,"# ##### ##### #").

Neither works. How do I set this?

Rick
 
Probably not the best, but you could just parse the string yourself and
insert the spaces where needed. Something like:

e.Value = e.Value.SubString(0,1) & " " & e.Value.Substring(1, 5) & " "
& e.Value.Substring(6, 5) & " " & e.Value.Substring(11, 1)

Thanks,

Seth Rowe
 
Hi,

e.Value = Format(CInt(e.Value), "0 00000 00000 0")

However, the row of a new addition is influenced in this method, too.
 
Thank you Seth and Yuichiro,

It seems I will have to parse the string and add my spaces manually like
Seth suggests.

The solution from Yuichiro will not work becuase many UPC strings start with
"0" i.e. 0 12345 23456. If I convert this to integer I loose the first "0"
so the formatted string would be "12345 23456" and not "0 12345 23456"

Rick
 
Hi,Rick
The solution from Yuichiro will not work becuase many UPC strings start
with "0" i.e. 0 12345 23456. If I convert this to integer I loose the
first "0" so the formatted string would be "12345 23456" and not "0 12345
23456"

In my way, it is not "# ##### ##### #", but "0 00000 00000 0".
"0" are sure to remain.
 
Yes, thank you Yuichrio

That does work. I think I tried the # ###### before and lost the "0", but
with your method it is always there.

Rick
 
When I run Yuichiro's sample I don't lose the leading zero, maybe you
typed it wrong?

Also, you might note that my code is almost twice as fast because it
avoids the using an integer conversion (although we're talking
nanoseconds here - I had to loop through each method 1,000,000 times to
get significant results). Also, if you use Yuichiro's code you might
need to use CLng instead of CInt to prevent the possible overflow issue
when you convert the string.

Thanks,

Seth Rowe
 
Hi,Seth
Also, if you use Yuichiro's code you might
need to use CLng instead of CInt to prevent the possible overflow issue
when you convert the string.

Oh,I blundered.
Integer is from -2,147,483,647 to 2,147,483,647.

Thanks.
 
Yes, I caught that on the first pass.

I used CULng in my app since a UPC/EAN is always an unsigned number.

Rick
 
Back
Top