Friday, March 5, 2010

Importing CSV file in a Datagrid in C#

Here is the code for Importing a csv file in the Datagrid.

There are 2 helper methods 'readFile' and 'generateDataTable'

readFile opens the csv and returns its contents.

private string readFile(string filePath)
{

StreamReader streamReader = new StreamReader(filePath);
return streamReader.ReadToEnd();

}

generateDataTableFromCSV generates and returns a DataTable for the provided comma seperated string.

private DataTable generateDataTableFromCSV(string fileContent)
{

DataTable dt = new DataTable();
string[] row = fileContent.Split("\r\n".ToCharArray());

string rowstr = row[0];

string[] col = rowstr.Split(',');
int colCount = 1;
foreach (string colstr in col)
{
dt.Columns.Add(new DataColumn("Column" + colCount));
colCount++;
}

dt.AcceptChanges();
return dt;
}

Here is the code on button click event handler that reads the csv file, and displays it in a datagrid.

private void btnLoad_Click(object sender, EventArgs e)
{
string fileContent = this.readFile(openFileDialog1.FileName);

DataTable dt = this.generateDataTableFromCSV(fileContent);

string[] row = fileContent.Split("\r\n".ToCharArray());
foreach(string rowstr in row)
{
DataRow myRow = dt.NewRow();
int colCount = 0;
string [] col = rowstr.Split(',');

foreach (string colstr in col)
{
myRow[colCount] = colstr;
colCount++;
}

dt.Rows.Add(myRow);
}

gvData.DataSource = dt;

}


For StreamReader we need to use the System.IO;
using System.IO;

here is a screenshot of the form


Feel free to ask me questions and report problems in the C# code.

10 comments:

  1. Mr. Umais this post really help me a lot. Can u please guide me how can i get a CVS file from a browser a load it in a grid. i tried it but unable to do so. wait for ur help!

    ReplyDelete
  2. Here you go Malik. I have modified the code to load a generic csv file.

    ReplyDelete
  3. Great !!!! I can't believe it is this much easy ... double thumbs up for you

    ReplyDelete
  4. how would I add new columns to DataGrid? and write them back to CSV?

    appreciate your help..

    ReplyDelete
  5. Humair I have already posted how to import CSV file and in the next post you can see how to Export Datagrid back to CSV file, I just posted the third post in which you can have the method by which you can add column to datagrid. Please let me know if you steel feel any confusion.

    ReplyDelete
  6. http://poetprogramming.blogspot.com/2010/03/adding-columns-to-a-csv-using-datagrid.html

    for adding column(s) in the CSV file through datagrid..

    ReplyDelete
  7. Umais i m stuck in loading data from database and save it as csv...can u help me in that? also do u have any idea abt DLL .. :(

    ReplyDelete
  8. Sameer sorry I was bit busy in my work, however you can find the code for uploading the data in Datagrid from the data, and Use my previous post for saving it to CSV file.. it will be the same, for DLL I am writing the code for it, and will post it, the day after tomorrow..

    http://poetprogramming.blogspot.com/2010/04/laoding-data-from-database-in-data-grid.html

    ReplyDelete
  9. umais its a great article......thanks for this it really has helped me get out of a mischevious situation. Thanks

    ReplyDelete
  10. Umais I thank you very much for such a grate code. Your code helped me in learning javascript. Thanks a million

    ReplyDelete