ARTICLE

Import Data from Text and CSV file to DataGridView in .Net

Posted by Ankur Gupta Articles | ASP.NET Controls in C# July 23, 2010
In this article we will see how to import data from text and CSV file to DataGridView in .Net.
Reader Level:
Download Files:
 

First add a browser control, TextBox, Button and a DataGridView Control on form

Add the following code in Button Click Event

Form1.JPG

private void button1_Click_1(object sender, EventArgs e)

{

    OpenFileDialog fdlg = new OpenFileDialog();

    fdlg.Title = "Select file";

    fdlg.InitialDirectory = @"c:\";

    fdlg.FileName = txtFileName.Text;

    fdlg.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";

    fdlg.FilterIndex = 1;

    fdlg.RestoreDirectory = true;

    if (fdlg.ShowDialog() == DialogResult.OK)

    {

        txtFileName.Text = fdlg.FileName;

        Import();

        Application.DoEvents();

    }

}


When you select any Text or CSV file from Browser page then you get the full path of Text or CSV file.

a1.jpg

You will pass this path in the following function that will return the dbf file data in a dataset.

public static DataTable GetDataTable(string strFileName)
{
    ADODB.
Connection oConn = new ADODB.Connection();
    oConn.Open(
"Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
    
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
    ADODB.
Recordset rs = new ADODB.Recordset();
    System.Data.OleDb.
OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
    
DataTable dt = new DataTable();
    rs.Open(strQuery,
"Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
        ADODB.
CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
    adapter.Fill(dt, rs);
    
return dt;
}

You can easily show dataset data in Datagridview.

private void Import()
{
    
if (txtFileName.Text.Trim() != string.Empty)
    {
        
try
        {
            
DataTable dt = GetDataTable(txtFileName.Text);
            dataGridView1.DataSource = dt.DefaultView;
        }
        
catch (Exception ex)
        {
            
MessageBox.Show(ex.Message.ToString());
        }
    }
}

Output:

 Form3.JPG

Login to add your contents and source code to this article
Article Extensions
Contents added by Mariammal G on Oct 10, 2012
post comment
     

You just set Target platform X86 of your application and us jet engine with VS10

Posted by Ankur Gupta Apr 12, 2013

This project uses the jet engine, which is not available for VS10. Microsoft has no intention of continuing support for the jet engine. How would this be rewritten without the jet engine, using LINQ, perhaps?

Posted by alice oberfoell Apr 06, 2013

i just tried running this and i get the following error message,"Provider cannot be found. it may not be properly installed."any ideas?

Posted by Rob C Mar 04, 2013

hi Ankur correct url is: https://www.defercode.com/tools/csharp-to-vb.aspx

Posted by Hirendra Sisodiya Jan 05, 2013

Hi Toni Thanks.. you can easily convert above example in vb.net by sevral open source for ex. https://www.defercode.com/tools/csharp-to-vb.aspx

Posted by Ankur Gupta Dec 20, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts