找了一天的相关材料,终于搞定了,现在和大家分享一下:
首先:把不需要的列删除了
try
{
dataGridView_import.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
dataGridView_import.CurrentCell.Selected = true;
if (dataGridView_import.Columns.Count > 0)
{
if (MessageBox.Show("Are you sure to delete the Entire Column?!", "Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
string columnname = dataGridView_import.Columns[dataGridView_import.CurrentCell.ColumnIndex].Name.ToString();
dataGridView_import.Columns.Remove(columnname);
}
}
else
{
deleteEntireColumnToolStripMenuItem.Enabled = false;
}
dataGridView_import.SelectionMode = DataGridViewSelectionMode.CellSelect;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
然后:借用dataadaptor,把datagridview中的数据装到dataset的datatable里面:
OleDbCommand mycom = new OleDbCommand(sqlstr, conn);
OleDbDataAdapter oledataadap = new OleDbDataAdapter(mycom);
DataSet ds = new DataSet();
oledataadap.Fill(ds,"Table");
DataTable dt = ds.Tables["Table"];
dataGridView_Survey_data.DataSource = dt;
int totalcolumn = dataGridView_import.RowCount;
for (int i = 0; i < totalcolumn; i++)
{
DataRow temprow = dt.NewRow();
temprow["D"] = depthmdv;
temprow["td"] = tvdv;
temprow["RO"] = ropv;
temprow["WO"] = wohv;
temprow["WB"] = wobv;
temprow["RM"] = rpmv;
temprow["fr"] = flowv;
temprow["SP"] = sppv;
temprow["TQ"] = tqv;
temprow["Muty"] = mudv;
temprow["Comments"] = commentsv;
dt.Rows.Add(temprow);
}
OleDbCommandBuilder oledbcb = new OleDbCommandBuilder(oledataadap);
DataSet datasetchange = ds.GetChanges();
if (datasetchange != null)
{
oledataadap.Update(datasetchange);
label1.Text = "Saving to DataBase...";
ds.AcceptChanges();
label1.Text = "Saved to DataBase!!!";
}
#endregion
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
label1.Text = ex.Message;
}
finally
{
mycom.Dispose();
conn.Dispose();
}
这里面的 depthmdv和其他的变量在前面赋值: =Convert.ToSingle( dataGridView_import.Rows[i].Cells[inde].Value);
后面用到OleDbCommandBuilder的一些方法,不是很熟,也不敢多说,只是自己试着使用一下,觉得还行,基本可以实现自己的设想。。。。。