By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Format string in DataGrid

P: n/a
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
Oct 30 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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:
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
Oct 31 '06 #2

P: n/a
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.(^^;
Oct 31 '06 #3

P: n/a
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
Oct 31 '06 #4

P: n/a
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.(^^;

Oct 31 '06 #5

P: n/a
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:
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.(^^;
Oct 31 '06 #6

P: n/a
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
Oct 31 '06 #7

P: n/a
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.(^^;
Oct 31 '06 #8

P: n/a
Yes, I caught that on the first pass.

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

Rick

"Yuichiro Ochifuji" <oc******@japan.interq.or.jpwrote in message
news:eU**************@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.(^^;

Oct 31 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.