anyone could help me out with a snippet?
I have a Tab delimited .CSV.
I want to parse the words, delimited by tabs, into a seperate Array for everyone.
File looks like this:
SKU Name Available ShortDescription
FirstLine only have column Names.
Sunny SharmaPosted Jul 1, 2013, 7:44 AM
Well, you can do so using Regex.Split() method, but this way you will fail to maintain the consistency of data. There are chances of you might mess up the data for a particular SKU with the details of some other SKU. Hence I still would suggest you to use Oledb Reader.
Below is a solution for you to import a text file directly into a DataTable:
---------------------------------------------------------------------
static DataTable ImportDelimitedFile(DataTable dt, string filename, string delimiter)
{
StreamReader file = new StreamReader(filename); string line;
while ((line = file.ReadLine()) != null) { if (line.Trim().Length > 0) {
string[] columns = line.Split(delimiter.ToCharArray(), StringSplitOptions.None);
if (dt.Columns.Count == 0)
{
foreach (string str in columns)
{ dt.Columns.Add(str); }
continue;
}
else
{
DataRow dr = dt.NewRow();
for (int i = 0; i < columns.Length;i++ )
{
dr[i] = columns[i];
}
dt.Rows.Add(dr);
}
}
}
return dt;
}
---------------------------------------------------------------------------
Use it like this:
------------------------------------------------------------
DataTable dt = new DataTable();
dt = ImportDelimitedFile(dt,"D:\\testData.txt", " ");
myDataGridView.DataSource=dt;
----------------------------------------------------------------------------------
Add References to System.Data.Oledb, System.Data & System.IO if haven't done yet.
Mark this as answer if it helps.
Cheers!
jason smithPosted Jul 8, 2013, 9:09 AM
Here is an inexpensive library that can create CSV files from data tables or generic list of objects:
http://www.kellermansoftware.com/p-50-csv-reports.aspx
BASCHDIPosted Jul 1, 2013, 6:45 AM
The File i got is not delimited with tab, just Whitespaces.
And its a .txt , not CSV. (sry)
I need to split the strings with regex then put the splitet strings, every one, in one seperate array.
Like this:
List list = new List();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
Sunny SharmaPosted Jul 1, 2013, 6:19 AM
How about importing that CSV file directly to a DataTable and then bind this dataTable as DataSource for a DataGridView.
Use the method below:
-----------------------------------------------------
static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
{
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path);
string sql = @"SELECT * FROM [" + fileName + "]";
using (OleDbConnection connection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command)) {
DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable); return dataTable; } }
-----------------------------------------------------
Use it like this:
---------------------------------------
DataTable dt = GetDataTableFromCsv("D:\\test.csv", true);
myDataGridView.DataSource=dt;
---------------------------------------
As simple as that :)
Happy Coding.
Do mark it as answer if it helps.
Cheers!
BASCHDIPosted Jul 1, 2013, 6:04 AM
Sunny SharmaPosted Jul 1, 2013, 5:59 AM