Tejas Kullarwar

Tejas Kullarwar

  • NA
  • 4
  • 21.8k

Datagridview not showing data

Mar 26 2013 7:14 AM
I am trying to import some data from excel to DGV(datagridview). I am using this below mentioned code which works perfectly fine upto the point that the datatable is being filled with the required data, but the data grid is not showing anything. can anyone please point out the issue here?

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();
            }
        }}}


Answers (2)