string delimiter = ","; string tablename = "Table"; DataSet dataset = new DataSet(); OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; if (openFileDialog1.ShowDialog() == DialogResult.OK) { filename = openFileDialog1.FileName; StreamReader sr = new StreamReader(filename); string csv = File.ReadAllText(openFileDialog1.FileName); dataset.Tables.Add(tablename); dataset.Tables[tablename].Columns.Add("Stock Number"); dataset.Tables[tablename].Columns.Add("Order Number");
string allData = sr.ReadToEnd(); string[] rows = allData.Split("\r".ToCharArray());
foreach (string r in rows) { string[] items = r.Split(delimiter.ToCharArray()); dataset.Tables[tablename].Rows.Add(items); } this.dataGridView1.DataSource = dataset.Tables[0].DefaultView; } }
public string filename { get; set; }