P
psifreak
Guest
Im using the following to browse through folders to find the CSV file I need (big function, somewhat sloppy right now).
It pulls the last record and sticks it in the DGV with no problems, but the file I clicked has 4 records. I tried just setting the datasource but these files dont have headers, hence the adding rows one at a time (just in case someone wonders). Anybody know why its not catching any of the other rows in the file? Im tinkering with using csvReader.ReadLine() instead of ReadFields() but for unknown reasons I get the red squiggles when I swapped that.
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "csv files (*.csv)|*.csv"; //|All files (*.*)|*.*
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.Columns.Clear();
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
DataTable csvData = new DataTable();
try
{
string s = openFileDialog1.FileName;
using (TextFieldParser csvReader = new TextFieldParser(s))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
//read column names
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData == "")
{
fieldData = null;
}
}
csvData.Rows.Add(fieldData);
dataGridView1.ColumnCount = csvData.Columns.Count;
dataGridView1.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
May the fleas of a thousand camels feast happily on the lower regions of your enemies. And may their arms be too short to scratch!
Continue reading...
It pulls the last record and sticks it in the DGV with no problems, but the file I clicked has 4 records. I tried just setting the datasource but these files dont have headers, hence the adding rows one at a time (just in case someone wonders). Anybody know why its not catching any of the other rows in the file? Im tinkering with using csvReader.ReadLine() instead of ReadFields() but for unknown reasons I get the red squiggles when I swapped that.
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "csv files (*.csv)|*.csv"; //|All files (*.*)|*.*
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.Columns.Clear();
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
DataTable csvData = new DataTable();
try
{
string s = openFileDialog1.FileName;
using (TextFieldParser csvReader = new TextFieldParser(s))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
//read column names
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData == "")
{
fieldData = null;
}
}
csvData.Rows.Add(fieldData);
dataGridView1.ColumnCount = csvData.Columns.Count;
dataGridView1.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
May the fleas of a thousand camels feast happily on the lower regions of your enemies. And may their arms be too short to scratch!
Continue reading...