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 Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.IO;
using System.Data.OleDb;
using System.Configuration;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Data.DataTable ds = new System.Data.DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataGridView dataGridView1 = new DataGridView();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var datagrid = new Form3(); // Form 3 has the data grid view
datagrid.Show();
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "Project Sunset_PLS Lien 2 Strats.xlsx");
string Connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + @";Extended Properties=""Excel 12.0
Macro;HDR=Yes;ImpoertMixedTypes=Text;TypeGuessRows=0""";
OleDbConnection conn = new OleDbConnection(Connectionstring);
string command_check = "Select * from [Strats$]";
OleDbCommand cmd = new OleDbCommand(command_check, conn);
try
{
conn.Open();
ds.Clear();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds; // I can see data filled in ds, but datagrid is not showing anything
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}}}
Tejas KullarwarPosted Mar 26, 2013, 7:36 AM
I changed one more thing... I dont know whether I was doing that correct or worng:
If you observe I was showing the grid at the start of the programme. Now I shifted that statement: "datagrid.Show();" to the end. And one more thing is this correct?
DataGridView dataGridView1 = new DataGridView();
var datagrid = new Form3(); // Form 3 has the data grid view
datagrid.Show();
Vishal GilbilePosted Mar 26, 2013, 7:25 AM
From my point of view just try to replace the below line
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
With the following.
dataGridView1.DataSource=ds.DefaultView.
Hope that solves your problem.
With Regards,
Vishal Gilbile.