Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
I do not see what's wrong.

C#
foreach (DataRow row in dataSet.Tables[0].Rows)
{
    string tmp = String.Format("{0}", row["PZN"].ToString().Trim().PadLeft(7, '0'));
    row["PZN"] = tmp;
    Console.WriteLine(tmp);
    Console.WriteLine(row["PZN"]);
}


gives me output like...
0930673<br />
930673<br />
0148814<br />
148814


Note the missing zeros while printing the row. tried dataSet.Tables[0].AcceptChanges(); no effect. DataSet comes from Excel-File using Microsoft.ACE.OLEDB.12 with about 110 results and is visualized in DataGridView. Everything is working fine except the reassignment/reformatting of the rows.

IMHO this is suppose to work.

Thanks, guys.
Posted
Updated 6-Oct-11 10:15am
v2

My guess is that your row["PZN"] holds an Integer. Your String is successfully parsed as an Integer and will not throw an Exception, but the first 0 gets lost in the conversion ;)
 
Share this answer
 
Comments
Manfred Rudolf Bihy 7-Oct-11 10:48am    
There you go, have a 5!
Sander Rossel 7-Oct-11 12:54pm    
Thanks! :)
This is probably a little difficult to explain, but row["PZN"] is an object - not anything derived from an object. It is also a fairly special object in that when you assign to it, it gets converted to the underlying type for the DataTable - in this case, int, I suspect from the name. So, when you read it back and ToString it, the underlying datatype is used again, and a standard int-to-string convertion is done.
Try it yourself:
C#
int i = 0;
foreach (DataRow row in dt.Rows)
    {
    string s = i.ToString().PadLeft(7, '0');
    i++;
    object o = s;
    row["myInt"] = s;
    Console.WriteLine("{0}:{1}:{2}", s, o.ToString(), row["myInt"]);
    row["myStr"] = s;
    Console.WriteLine("{0}:{1}:{2}", s, o.ToString(), row["myStr"]);
    }

The results are:
0000000:0000000:0
0000000:0000000:0000000
0000001:0000001:1
0000001:0000001:0000001
0000002:0000002:2
0000002:0000002:0000002
 
Share this answer
 
Comments
Sander Rossel 7-Oct-11 2:33am    
My 5. It is what I said, but more detailed. And seeing you have solution 2 and I have solution 3 I think we may have posted our answers at the same time.
Unfortunately someone has been giving us 1-votes for a perfectly fine answer (giving me a whooping -16 to rep! :| ), so 5 to negate that vote too ;)
OriginalGriff 7-Oct-11 5:01am    
Yes, some idiots do things like that. It doesn't matter, and I have compensated your univote. Generally you find that compensated votes end up increasing your rep more than the original uni-vote damaged it, so it seems both pointless and rude to univote without any explanation...:laugh:
Sander Rossel 7-Oct-11 7:04am    
-16 rep... +40 rep :laugh:
Thanks, and I totally agree with you :)
To bad you can still downvote without having to give an explanation.
Manfred Rudolf Bihy 7-Oct-11 10:49am    
My 5 too!
Perhaps (and this is just kind of a guess) its because tmp is declared explicitly as a string, and row["PZN"] isn't?
 
Share this answer
 
dataSet.Tables[0].Columns["PZN"].DataType = System.Type.GetType("System.String");

fixed it, column was set to decimal or double.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900