Saving data to text file and loading it into datagrid in C #
3 answers
It is not clear from your question where the data starts from, or why you need a text file. However, I will answer one of your questions. There are many ways to read a text file. This is how I usually do it:
First write the file with the schema
using (StreamWriter sw = new StreamWriter(sPath + @"\schema.ini"))
{
sw.WriteLine("[" + sFile + "]");
sw.WriteLine("ColNameHeader=False");
sw.WriteLine("Format=FixedLength");
sw.WriteLine("Col1=CO_ID Text Width 2");
sw.WriteLine("Col2=AGENCY_CD Text Width 10");
// lines for additional columns here
sw.Close();
sw.Dispose();
}
Then read the data into the DataSet using ODCB.
string cs = @"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" + sPath;
OdbcConnection cn = new OdbcConnection(cs);
string q = @"select * from [" + sNewFN + "]";
OdbcDataAdapter da = new OdbcDataAdapter(q, cn);
da.Fill(ds, "MyTable");
Ds.Tables ["MyTable"] is the DataSource for the DataGrid
There is information about this method here:
http://msdn.microsoft.com/en-us/library/ms714091%28v=VS.85%29.aspx
+1
a source to share