I want to get all the data in a excel sheet and put it in a datagridview to be able to get specific information from the excel file. This will later be sent to a web service, but right now I only have problems to add the data to datagridview so I can see the data easier. The problem is that when I debug, I see that the program never enters the "foreach" iteration.
private void xlsWatcher_Created(object sender,
System.IO.FileSystemEventArgs e)
{
string VIN;
string modelCode;
string[] optionCodes;
int rowNr = 5;
int nrOfOptions = 0;
OleDbConnection conn = new System.Data.OleDb.OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + e.FullPath +"; " + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("select * from [Rapport 1$]", conn);
DataSet ds = new DataSet();
ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
// Access particular cells on DataGridView.
foreach (DataGridViewRow row in dataGridView1.Rows) /////JUMPS OVER THIS ITERATION!!!!!!
{
VIN = dataGridView1.Rows[rowNr].Cells[1].ToString();
modelCode = dataGridView1.Rows[rowNr].Cells[2].ToString();
//Count all the optionCodes that is on the row
if (dataGridView1.Rows[rowNr].Cells[3].Value != DBNull.Value)
{
optionCodes = new string[5];
optionCodes[0] = dataGridView1.Rows[rowNr].Cells[3].ToString();
nrOfOptions += 1;
optionCodes[1] = dataGridView1.Rows[rowNr].Cells[4].ToString();
nrOfOptions += 1;
optionCodes[2] = dataGridView1.Rows[rowNr].Cells[5].ToString();
nrOfOptions += 1;
optionCodes[3] = dataGridView1.Rows[rowNr].Cells[6].ToString();
nrOfOptions += 1;
optionCodes[4] = dataGridView1.Rows[rowNr].Cells[7].ToString();
string[] elOptions = optionCodes[4].Split(new Char[] { ' ' });
nrOfOptions = nrOfOptions + elOptions.Length;
MessageBox.Show(Convert.ToString(elOptions.Length));
}
}
}
Thank you in advance.
Baris
Loading

SenthilkumarRank #387 · 6.5K reputationPosted Apr 27, 2012, 2:08 AM EDT
http://www.codeproject.com/Questions/137431/How-to-get-data-from-excel-sheet-to-datagridview-i
http://www.dotneter.com/import-excel-file-into-datagridview
http://vb.net-informations.com/datagridview/vb.net_datagridview_import.htm
Baris Turkoglu14 reputationPosted Apr 27, 2012, 2:20 AM EDT
Can you please check the code I've posted and see what I'm doing wrong?
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 27, 2012, 2:33 AM EDT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Retrieving_excel_data_to_combobox
{
public partial class Form1 : Form
{
public OleDbConnection con;
public void pintu(string s)
{
con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 8.0;");
}
public OleDbCommand com;
public DataSet ds;
public OleDbDataAdapter oledbda;
public DataTable dt;
public string str;
public Form1()
{
InitializeComponent();
}
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.ShowDialog();
openfiledialog1.Filter = "allfiles|*.xls";
TextBox1.Text = openfiledialog1.FileName;
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text = "Please select";
}
private void btndisplay_Click(object sender, EventArgs e)
{
pintu(TextBox1.Text);
try
{
con.Open();
str = "select * from [sheet1$]";
com = new OleDbCommand(str, con);
ds = new DataSet();
oledbda = new OleDbDataAdapter(com);
oledbda.Fill(ds, "[sheet1$]");
con.Close();
DataGridView1.DataSource = ds;
DataGridView1.DataMember = "[sheet1$]";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Thanks
Baris Turkoglu14 reputationPosted Apr 30, 2012, 12:45 AM EDT
Couldn't you give me an example on my code instead of creating your own code?
Cause your code doesn't do the same as mine.
Best regards
Baris
Kunal VaishyaRank #990 · 2K reputationPosted Apr 30, 2012, 12:54 AM EDT
http://www.c-sharpcorner.com/Blogs/8808/how-to-open-excel-file-in-datagridview.aspx
Baris Turkoglu14 reputationPosted Apr 30, 2012, 1:44 AM EDT
Do I have to update my datagridview1 somehow after setting the Datasource or?