'How to load strings with DataTable.Load() not as numbers?
I have the following code:
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dataTable = ds.Tables.Add("Utilizatori");
Microsoft.Office.Interop.Excel.Range range = Globals.Sheet13.Range["A7", "D12"];
Microsoft.Office.Tools.Excel.ListObject listObject = Globals.Sheet13.Controls.AddListObject(range, "Utilizatori");
try
{
dataTable.Clear();
using (var cmd = new MySqlCommand())
{
cmd.Connection = mySqlConnection;
cmd.CommandText = "SELECT id, telefon, e_mail, worksheet_name, adresa, nume FROM consumatori";
MySqlDataReader reader = cmd.ExecuteReader();
dataTable.Load(reader, System.Data.LoadOption.OverwriteChanges);
listObject.AutoSetDataBoundColumnHeaders = true;
listObject.SetDataBinding(ds, "Utilizatori");
}
}
catch (Exception exception)
{
System.Windows.Forms.MessageBox.Show(exception.Message);
}
From the field "telefon" I would like to retrieve the numbers with starting 0s in the numbers.
For now, my Excel table brings in the "07xxxxxxxx" string as a number like 7xxxxxxxx. I can't figure this one out, the load method converts it into a number, or does the Excel Sheet formatting converts the string into a number?
Thanks for the help!
Solution 1:[1]
It turns out Excel Sheet was the one to blame. I was pulling in numbers, starting with 0, and he formatted them to numbers.
foreach (Microsoft.Office.Interop.Excel.ListColumn column in listObject.ListColumns)
{
column.Range.Cells.NumberFormat = "@";
}
By setting the formatting to text it shows up correctly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bogdan - Daniel Mihalcea |
