0
我正在创建windows应用程序,以便将Excel文件中的数据插入到数据库中。我的应用程序工作正常,但在将excel文件中的数据显示到数据网格视图并插入数据库之后,它会插入一个空行。从Excel文件中读取数据
这里是我的代码,
//we need to copy the data from datagridview into data table
DataTable dtable = new DataTable();
dtable.TableName = "PRODUCTS";
//we need to copy column from datagridview into data table
foreach (DataGridViewColumn col in ExcelDataGV.Columns)
{
dtable.Columns.Add(col.DataPropertyName, col.ValueType);
}
//we need to copy rows from datagridview into data table
foreach (DataGridViewRow row in ExcelDataGV.Rows)
{
if (row.IsNewRow)
continue;
DataRow dtRow = dtable.NewRow();
for (int i = 0; i < ExcelDataGV.Columns.Count; i++)
dtRow[i] = (row.Cells[i].Value == null ? DBNull.Value : row.Cells[i].Value);
dtable.Rows.Add(dtRow);
}
//foreach loop will read data from data table and insert/add those value in database
foreach (DataRow dr in dtable.Rows)
{
}
我需要修改我的循环语句?
您显示的代码不会被编译。一个'foreach'循环通常包含一个body。你介意显示它?事实上,在这个问题关闭之前,你介意显示一些实际的代码? – 2011-05-02 20:12:28
@Darin:我编辑了我的问题。 – user735052 2011-05-02 20:15:28