Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGridView doesn't appear on screen

DataGridView doesn't appear on screen

Scheduled Pinned Locked Moved C#
databasecsharplinqquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RickSharp
    wrote on last edited by
    #1

    Hey guys, Easy one here. I want to see the results of my datatable in the DataGridView. When i execute, it runs without errors but no DataGridView shows up on screen. Does DataGridView require a form to view the results or something? What am I missing?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using FileHelpers;
    using System.Data; //not used by default
    using System.IO; //not used by default
    using System.Data.OleDb; //not used by default
    using System.Windows.Forms;

    namespace CSVParser
    {
    class CSVParser
    {
    public static DataTable ParseCSV(string path)
    {
    if (!File.Exists(path))
    return null;

            string full = Path.GetFullPath(path);
            string file = Path.GetFileName(full);
            string dir = Path.GetDirectoryName(full);
    
            //create the "database" connection string 
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
              + "Data Source=\\"" + dir + "\\\\\\";"
              + "Extended Properties=\\"text;HDR=No;FMT=Delimited\\"";
    
            //create the database query
            string query = "SELECT \* FROM " + @"C:\\Users\\rsharp\\Desktop\\CustomerExport.csv";
    
            //create a DataTable to hold the query results
            DataTable dTable = new DataTable();
    
            //create an OleDbDataAdapter to execute the query
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
    
            //try
           // {
                //fill the DataTable
                dAdapter.Fill(dTable);
           // }
            //catch (InvalidOperationException /\*e\*/)
           // { }
    
            //the DataGridView
            DataGridView dgView = new DataGridView();
    
            //BindingSource to sync DataTable and DataGridView
            BindingSource bSource = new BindingSource();
    
            //set the BindingSource DataSource
            bSource.DataSource = dTable;
    
            //set the DataGridView DataSource
            dgView.DataSource = bSource;
    
            //Dispoe of the adapter
            dAdapter.Dispose();
    
            return dTable;
        }
    }
    

    }

    L 1 Reply Last reply
    0
    • R RickSharp

      Hey guys, Easy one here. I want to see the results of my datatable in the DataGridView. When i execute, it runs without errors but no DataGridView shows up on screen. Does DataGridView require a form to view the results or something? What am I missing?

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using FileHelpers;
      using System.Data; //not used by default
      using System.IO; //not used by default
      using System.Data.OleDb; //not used by default
      using System.Windows.Forms;

      namespace CSVParser
      {
      class CSVParser
      {
      public static DataTable ParseCSV(string path)
      {
      if (!File.Exists(path))
      return null;

              string full = Path.GetFullPath(path);
              string file = Path.GetFileName(full);
              string dir = Path.GetDirectoryName(full);
      
              //create the "database" connection string 
              string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
                + "Data Source=\\"" + dir + "\\\\\\";"
                + "Extended Properties=\\"text;HDR=No;FMT=Delimited\\"";
      
              //create the database query
              string query = "SELECT \* FROM " + @"C:\\Users\\rsharp\\Desktop\\CustomerExport.csv";
      
              //create a DataTable to hold the query results
              DataTable dTable = new DataTable();
      
              //create an OleDbDataAdapter to execute the query
              OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
      
              //try
             // {
                  //fill the DataTable
                  dAdapter.Fill(dTable);
             // }
              //catch (InvalidOperationException /\*e\*/)
             // { }
      
              //the DataGridView
              DataGridView dgView = new DataGridView();
      
              //BindingSource to sync DataTable and DataGridView
              BindingSource bSource = new BindingSource();
      
              //set the BindingSource DataSource
              bSource.DataSource = dTable;
      
              //set the DataGridView DataSource
              dgView.DataSource = bSource;
      
              //Dispoe of the adapter
              dAdapter.Dispose();
      
              return dTable;
          }
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      RickSharp wrote:

      Doesn DataGridView require a form to view the results or something?

      Yes, it does. It needs to be assigned to the Controls collection of the form that it's supposed to show on. Where's the entry-point of your application?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      R 1 Reply Last reply
      0
      • L Lost User

        RickSharp wrote:

        Doesn DataGridView require a form to view the results or something?

        Yes, it does. It needs to be assigned to the Controls collection of the form that it's supposed to show on. Where's the entry-point of your application?

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

        R Offline
        R Offline
        RickSharp
        wrote on last edited by
        #3

        By entry point do you mean is it just a win form, exe or class library? If so I have added a form project and console application project to my solution. So I have a class library that contains that code. I added a reference to that class library in the new forms and console projects that I created. I tried following this tutorial with no success. http://www.dotnetperls.com/datagridview-tutorial[^]

        L 1 Reply Last reply
        0
        • R RickSharp

          By entry point do you mean is it just a win form, exe or class library? If so I have added a form project and console application project to my solution. So I have a class library that contains that code. I added a reference to that class library in the new forms and console projects that I created. I tried following this tutorial with no success. http://www.dotnetperls.com/datagridview-tutorial[^]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          RickSharp wrote:

          By entry point do you mean is it just a win form, exe or class library? If so I have added a form project and console application project to my solution. So I have a class library that contains that code. I added a reference to that class library in the new forms and console projects that I created. I tried following this tutorial with no success.

          The tutorial has some different code than you have; A console-application doesn't show a form usually. There's also no need for a separate class-library - simply put then in a normal forms-application.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups