I'm trying to read rows from an Excel file and display them in an
ASP.NET DataGridview. I am using C# for the code file. I am using
OleDb to read from the Excel file. The columns that contain text load
into the grid fine, but the columns that contain just numbers don't
show up at all. I tried converting the text of the cells to an
integer first, but I get an error for converting from a type DBNull.
Anybody who has any help at all, I would greatly appreciate it. Here
is the code I am using to read from the Excel and load the
Datagridview:
private void popGrid(String filename)
{
String sConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filename + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection objConn = new
OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM
[Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet dset = new DataSet();
objAdapter1.Fill(dset);
objConn.Close();
DataSet inSet = new DataSet();
DataTable inTable = new DataTable();
inTable.Columns.Add("BLnum");
inTable.Columns.Add("Pieces");
inTable.Columns.Add("Weight");
inTable.Columns.Add("Size");
inTable.Columns.Add("custRel");
inTable.Columns.Add("ssRel");
inTable.Columns.Add("palletEx");
inTable.Columns.Add("pckDate");
inTable.Columns.Add("ldFee");
for (int i = 11; i < dset.Tables[0].Rows.Count; i++)
{
DataRow inRow = inTable.NewRow();
DataRow row = dset.Tables[0].Rows[i];
if (row[0].ToString() != "")
{
inRow["BLnum"] = row[0].ToString();//A
inRow["Pieces"] = row[2].ToString();//C
inRow["Weight"] = row[3].ToString();//D
inRow["Size"] = row[4].ToString();//E
inRow["custRel"] = row[5].ToString();//F
inRow["ssRel"] = row[6].ToString();//G
inRow["palletEx"] = row[6].ToString();//H
inRow["pckDate"] = row[8].ToString();//I
inRow["ldFee"] = row[10].ToString();//K
inTable.Rows.Add(inRow);
}
}
inSet.Tables.Add(inTable);
loadGrid.DataSource = inSet;
loadGrid.DataBind();
}