Click here to Skip to main content
15,885,124 members
Home / Discussions / C#
   

C#

 
QuestionDataGridView doesn't appear on screen Pin
RickSharp21-Nov-12 12:44
RickSharp21-Nov-12 12:44 
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?


C#
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;
        }
    }
}


modified 21-Nov-12 19:21pm.

AnswerRe: DataGridView doesn't appear on screen Pin
Eddy Vluggen21-Nov-12 13:17
professionalEddy Vluggen21-Nov-12 13:17 
GeneralRe: DataGridView doesn't appear on screen Pin
RickSharp21-Nov-12 13:27
RickSharp21-Nov-12 13:27 
GeneralRe: DataGridView doesn't appear on screen Pin
Eddy Vluggen21-Nov-12 13:42
professionalEddy Vluggen21-Nov-12 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.