I have already had some data in datagrid view and i want to import fresh records from excel file in such a way that imported data shd append in datagridview.
The code I provided is only get data from excel file and import to dgv. What modification shd i made that it would append and keep previous data in datagridview.
Code:
private DataTable getDataFromXLS(string strFilePath)
{
try
{
string strConnectionString = ""; //Data Source=HARIS\SQLEXPRESS;Initial Catalog=MFSAS;User ID=akram
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConnectionString);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select * FROM [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
da = null;
return dt;
}
catch (Exception ex)
{
return null;
}
finally { }
}
private void btnImportData_Click(object sender, EventArgs e)
{
DataTable test = getDataFromXLS(@"c:\temp.xls");
if (test != null)
{
dgvViewJDRecords.DataSource = test;
}
}