Home All Groups Group Topic Archive Search About

Format string in DataGrid

Author
30 Oct 2006 10:27 PM
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

Author
31 Oct 2006 1:05 AM
rowe_newsgroups
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


Rick wrote:
Show quoteHide quote
> 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
Author
31 Oct 2006 2:18 AM
Yuichiro Ochifuji
Hi,

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

However, the row of a new addition is influenced in this method, too.


--
Yuichiro Ochifuji
JAPAN
I am not good at English.(^^;
Author
31 Oct 2006 1:03 PM
Rick
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
Author
31 Oct 2006 1:25 PM
Yuichiro Ochifuji
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.


--
Yuichiro Ochifuji
JAPAN
I am not good at English.(^^;
Author
31 Oct 2006 2:00 PM
Rick
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
Author
31 Oct 2006 2:03 PM
rowe_newsgroups
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


Yuichiro Ochifuji wrote:
Show quoteHide quote
> 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.
>
>
> --
> Yuichiro Ochifuji
> JAPAN
> I am not good at English.(^^;
Author
31 Oct 2006 2:28 PM
Yuichiro Ochifuji
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.


--
Yuichiro Ochifuji
JAPAN
I am not good at English.(^^;
Author
31 Oct 2006 2:56 PM
Rick
Yes, I caught that on the first pass.

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

Rick



Show quoteHide quote
"Yuichiro Ochifuji" <ochif***@japan.interq.or.jp> wrote in message
news:eUsSbjP$GHA.1500@TK2MSFTNGP05.phx.gbl...
> 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.
>
>
> --
> Yuichiro Ochifuji
> JAPAN
> I am not good at English.(^^;