Hi All,
i need assistance with reading an excel file, writing it to a datagrid (this i already have in my code) and then for each line in the datagrid i need the following.
if a cell value is xxx in column y then do....
for example:
i have a column called "vehicle/pedestrian" now i want the following:
for each line in the datagrid if cell value in this column (vehicle......) then do.......
[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Runtime.InteropServices;
namespace Datagridtest
{
public partial class Form1 : Form
{
private void BrowseBtn_Click(object sender, EventArgs e)
{
FilePathBrowse.Filter = "SIT Files (*.xls) | *.Xls";
if (FilePathBrowse.ShowDialog() == DialogResult.OK)
{
if (FilePathBrowse.FileName != "")
// FileNamePath.Text = FilePathBrowse.FileName;
FilePath.Text = FilePathBrowse.FileName;
}
}
// const string fileName = @"c:\testing.xls";
private DataTable dt;
public static string BuildExcelConnectionString(string Filename, bool FirstRowContainsHeaders)
{
return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=\"Excel 8.0;HDR={1};\"",
Filename.Replace("'", "''"),
FirstRowContainsHeaders ? "Yes" : "No");
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string fileName = FilePath.Text;
if (!File.Exists(fileName))
// if (!File.Exists(FilePathBrowse.FileName))
{
MessageBox.Show("Cannot find file");
return;
}
string connStr = BuildExcelConnectionString(fileName, true);
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("Select * From [Sheet1$]", conn))
{
using (OleDbDataReader dr = cmd.ExecuteReader())
{
if (dt != null)
dt.Dispose();
dt = new DataTable();
dt.Load(dr);
}
}
}
// dt.Columns.Add(new DataColumn("FilterColumn", typeof(int)));
//foreach (DataRow row in dt.Rows)
//{
// // MessageBox.Show("bla");
// // int filterCode = default(int);
// // if (row["Code"] == DBNull.Value)
// // filterCode = -1;
// // else
// // {
// // int code = Convert.ToInt32(row["Code"]);
// // if ((code >= 90) && (code < 100))
// // filterCode = 1;
// // else if ((code >= 100) && (code < 110))
// // filterCode = 2;
// // }
// // row["FilterColumn"] = filterCode;
// //}
//}
dataGridView1.DataSource = dt;
}
}
}
[/code]
the excel i have is something like this
[code]
point Long Lat FOV [pedestrian / vehicle] [single / multiple] Target Location LOS/Detection Freetext
1 -73.882644 40.692344 5 Vehicle Multi TGT Road MIN DET Range Your Fee text here
2 -73.882903 40.69259 2 Pedestrian Single TGT sidewalk DET from PREV to this PT Your Fee text here
[/code]
thanks