DataRowCollection.Add and NumberDecimalSeparator

  • Thread starter Thread starter Vyacheslav Lanovets
  • Start date Start date
V

Vyacheslav Lanovets

Hello, All!

I have a Column object with String DataType, so when it's assigned with
Double value it gets converted to String. Convertion uses default locale
which in my case has "," as NumberDecimalSeparator.
Assigning is perfomed by DataRowCollection.Add(object[])

I see two solutions:
- change current thread's Culture before calling Add() (I use this solution
now)
- change DataType of Column to double, so WriteXml of Dataset will write it
with neutral culture

Are there any other solutions? Somehow substitute neutral culture for
convertion?

My code:
------------
DataSet cnv_ds = new DataSet();
cnv_ds.ReadXmlSchema(m_convert_schema); // so all Columns are Strings :(
cnv_ds.Tables.Add("Converted");
foreach (DataRow row in xls_ds.Tables[0].Rows)
{
object[] ins_row = ConvertRow(row.ItemArray);
cnv_ds.Tables[0].Rows.Add(ins_row); // here I get 92,5 instead of 92.5
}
 
why not change the Locale of the DataTable? check out DataTable.Locale


--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.
 
Hello, Ollie!
You wrote on Tue, 22 Feb 2005 11:38:34 -0000:

OR> why not change the Locale of the DataTable? check out DataTable.Locale

Maybe I am not right, but this code will output the same for any locale:

CultureInfo nc = new CultureInfo("");
nc.NumberFormat.NumberDecimalSeparator = ","; // comment this out

Thread.CurrentThread.CurrentCulture = nc; // I'm trying to avoid this

DataSet dataSet = new DataSet("DoubleTest");
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("double", typeof(String)));

dataTable.Locale = new CultureInfo("ru-RU"); // ","
DataRow dataRow = dataTable.NewRow();
dataRow["double"] = 1.5;
dataTable.Rows.Add(dataRow);

dataTable.Locale = new CultureInfo("en-US"); // "."
dataRow = dataTable.NewRow();
dataRow["double"] = 2.5;
dataTable.Rows.Add(dataRow);

dataSet.Tables.Add(dataTable);
dataSet.WriteXml(Console.Out);
Console.WriteLine();



Regards, Vyacheslav
 
Back
Top