Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.80/5 (4 votes)
See more:
I have below code with me .

C#
private void button2_Click(object sender, EventArgs e)
      {
         //This code is for validating the csv file and filling the rows in the grid.
          string Path;
          Path = lblfilepath.Text;
          dataGridView1.DataSource = ReadCSV(Path);
          //Please help me here
      }
      private DataTable ReadCSV(string Filename)
      {
          string MyPath = System.IO.Path.GetDirectoryName(Filename);
          string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MyPath + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
          String todaysdate;
          todaysdate = DateTime.Today.ToString() ;
          string CmdText = "select * from " + System.IO.Path.GetFileName(Filename);
          OleDbConnection Con = new OleDbConnection(ConnectionString);
          OleDbDataAdapter adptr = new OleDbDataAdapter(CmdText, Con);

          //OleDbCommand Cmd = new OleDbCommand(CmdText + txtFileName.Text, Con);
          DataSet Ds = new DataSet();
          adptr.Fill(Ds);
          return Ds.Tables[0];
      }


On the comment line //Please help me here
I need to loop throught the Datagrid and read values from each column from each rows.

Please suggest me how can I make a combination of
DataCommand
DataReader
DataTable
DataAdaptor
Posted
Updated 19-Apr-11 22:07pm
v3

dataGridView1.Rows provides a collection of DataRow objects in the gridview. Just iterate that collection.
 
Share this answer
 
Comments
RDBurmon 13-Jan-11 13:53pm    
Thanks Buddy !!
John is right, if you need access to specific columns just use the .Cells object within the DataRow. You can access via field name or via the row's Items. For example,

foreach (DataRow x in dataGridView1.Rows)
{
//access data either way:
    x.Field<>();
    x.ItemArray;
}
 
Share this answer
 
Comments
RDBurmon 13-Jan-11 13:53pm    
Thanks Buddy !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900