Hi
I have a linq context to load an empty table into datagridview1.
I'm trying unsuccessfully to load my excel data into that datagridview.
the excel comes over to the a unbound table but not this bound one.
Help anyone
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using Excel;
namespace linqFoxExcelSql
{
public partial class Form1 : Form
{
// linq Context to Stats table - Stats is empty - want to fill with excel data
DataClasses1DataContext db = new DataClasses1DataContext();
public Form1()
{
InitializeComponent();
}
// open excedl and fill result with the data
DataSet result;
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
result = reader.AsDataSet();
cboSheet.Items.Clear();
foreach (System.Data.DataTable dt in result.Tables)
cboSheet.Items.Add(dt.TableName);
reader.Close();
}
}
}
// scbosheet changed get the records
private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.DataSource = result.Tables[cboSheet.SelectedIndex];
}
// use linq to attach or empty sql table - i want to fill this with excel data
private void Form1_Load(object sender, EventArgs e)
{
var Answer = from c in db.Stats
select c;
dataGridView1.DataSource = Answer;
}
}
}
Thanks
JereTheBear