Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Am trying to change the number format of a value in a data table. Am using String.Format("{0:n}", number) method to change the number format but it doesn't work.
Below is my code,
C#
foreach (DataRow dr in datasetx.Tables[0].Rows)
                  {

         dr["total"] = String.Format("{0:n}", Convert.ToDecimal(dr["total"]));

                  }
                  datasetx.Tables[0].AcceptChanges();


but it is not working.
dr["total"] = 123456789.1245;
String.Format("{0:n}", Convert.ToDecimal(dr["total"])); = 1,234,567,89.1245; as expected but value in data table is still the old one.

before : dr["total"] = 123456789.1245;
after formatting string using String.Format : dr["total"] = 123456789.1245;


why value is not reflecting in datatable ?
actually String.Format("{0:n}", ) function is working as expected but that value is not at all reflecting in data table.
Posted

This is a common mistake: you just counfound the decimal value (stored in the database) with its string representation.

At no point you should store a formatted value in the database. The database has to hold the number; only when you display this number to the user you apply a format to it.
 
Share this answer
 
Comments
Am Gayathri 29-Oct-15 6:03am    
Thanks for the replay. I fixed this problem by formatting the values while data binding in Grid view (Row data bound).
You need not to Call datasetx.Tables[0].AcceptChanges()
like this method will write formatted string.
C#
protected void Page_Load(object sender, EventArgs e)
   {
       DataTable dt = new DataTable();
       DataColumn dc = new DataColumn("total");
       dt.Columns.Add(dc);
       DataRow dr = dt.NewRow();
       dr["total"] = "123456789.1245";
       dt.Rows.Add(dr);

       foreach (DataRow dr1 in dt.Rows)
       {

           dr1["total"] = String.Format("{0:n}", Convert.ToDecimal(dr1["total"]));
       }
       Response.Write(dt.Rows[0]["total"]);
   }
 
Share this answer
 
v2
Comments
Am Gayathri 29-Oct-15 6:03am    
Thanks for the replay. I fixed this problem by formatting the values while data binding in Grid view (Row data bound).
Well, looking at your code:
According to the MSDN[^]:
AcceptChanges() Commits all the changes made to this table since the last time AcceptChanges was called.
When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.
The AcceptChanges method is generally called on a DataTable after you attempt to update the DataSet using the DbDataAdapter.Update method.

Meaning, your actual data is unaffected in the database. You're just updating the data in the datatable. If you want this change in the database, you need to write the update statement for that particular record.

-KR
 
Share this answer
 
Comments
Am Gayathri 29-Oct-15 6:03am    
Thanks for the replay. I fixed this problem by formatting the values while data binding in Grid view (Row data bound).
Have you called save changes method in your code?
 
Share this answer
 
Comments
Krunal Rohit 29-Oct-15 3:35am    
SaveChanges() is for EF.

-KR
Am Gayathri 29-Oct-15 6:03am    
Thanks for the replay. I fixed this problem by formatting the values while data binding in Grid view (Row data bound).

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