GridView & FormView dropping leading zeros from numbers set as strings

I'm storing in a SQL database a certain field (an employee number) as a varchar data type. Throughout the app (datasource, update/insert stored procedures, etc.) I'm working with it as a string. I've even ensured that it's being used as a string "behind the scenes" by watching it in SQL Profiler.

Problem is, the GridView & FormView that display it always drop any leading zeros. Wierder yet, when I put the GridView in Edit mode & add a zero, it saves to the database with the zero, but the leading zero doesn't display in the GridView!

Anyone know how I can fix this?


"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
0 capella07 9/5/2008 4:23:12 PM
While waiting for the thread to get approved I did some more testing. I've seen some other threads here regarding dropped leading zeros, but I didn't find anything that helped.

Another thing of note, I have another page build very similarly to this one (in fact the one I'm having trouble with was actually the template for the second one!) and there are four fields that I can enter leading zeros in and they save & display as they should; which means I'm probably missing something somewhere, but I don't have a clue where to look - I THOUGHT I looked everywhere, but obviously not!!!

"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
0 capella07 9/5/2008 4:50:33 PM

Hi capella07,

I haven't met this issue in my testing so far, but I think you can format it in RowDataBound event:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label lb = (Label)e.Row.FindControl("Label1");   //"Label1" is used to display "employee number" field
            lb.Text = int.Parse(lb.Text).ToString();


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0 Qin 9/10/2008 5:50:48 AM

Thread closed