DataGridView doesn't appear on screen
-
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; } }
}
-
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; } }
}
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![^]
-
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![^]
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[^]
-
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[^]
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![^]