Skip to content

Datagridview not showing data

Tejas Kullarwar

Asked Mar 26, 2013, 3:14 AM EDT2 answers
Share
LinkedInX
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();
            }
        }}}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Vishal Gilbile
    0
    Hello Friend,
                    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.
    0
  • 0
    Not working man....

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