这是我到目前为止的代码。表格填写正确。但是,如何将Edited行插入Valid表,然后从Edit表中删除它?我没有主键,因为表格是从Excel表格中导入的,只有3列名称,电子邮件和移动设备。
这是我到目前为止所做的:
protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="Update")
{
DataSet EditT = new DataSet();
DataSet ValidT = new DataSet();
if (Session["Edit"] != null)
{
EditT = (DataSet)Session["Edit"];
}
if (Session["Valid"] != null)
{
ValidT = (DataSet)Session["Valid"];
}
DataTable dtEdit = EditT.Tables[0];
DataTable dtValid = ValidT.Tables[0];
}
}

如果你想看看我在这里上传Excel表格是代码:
protected void UploadBtn_Click(object sender, EventArgs e)
{
string filename = string.Empty;
if(FileUpload1.HasFile)
{
try
{
string[] allowFile = { ".xls", ".xlsx" };
string fileExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isVaidFile = allowFile.Contains(fileExt);
if (!isVaidFile)
{
MessageLbl.ForeColor = System.Drawing.Color.Red;
MessageLbl.Text = "Must be an Excel file";
}
else
{
int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize <= 1048576)
{
filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName));
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadExcel/") + filename);
string filePath = Server.MapPath("~/UploadExcel/") + filename;
OleDbConnection con = null;
if (fileExt == ".xls")
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";");
}
else if (fileExt == ".xlsx")
{
con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
}
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();
string ExcelCommandText = @" SELECT * FROM [" + getExcelSheetName + @"]";
string ValidExcelCommandText = @"SELECT * FROM [" + getExcelSheetName + @"] where ((Email like '%@_%.com') and (Name is not null) and not ( ISNUMERIC(Mobile) = 0))";
string EditExcelCommandText = @" SELECT * FROM [" + getExcelSheetName + @"] where Email not like '%@_%.com' or Email is null or Name is null or Mobile is null or Mobile not like '^[[:digit:]]+$'";
OleDbCommand ExcelCommand = new OleDbCommand(ExcelCommandText, con);
OleDbCommand EditExcelCommand = new OleDbCommand(EditExcelCommandText, con);
OleDbCommand ValidExcelCommand = new OleDbCommand(ValidExcelCommandText, con);
OleDbDataAdapter EditAdapter = new OleDbDataAdapter(EditExcelCommand);
OleDbDataAdapter ValidAdapter = new OleDbDataAdapter(ValidExcelCommand);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataSet ExcelDataSet = new DataSet();
DataSet EditDataSet = new DataSet();
DataSet ValidDataset = new DataSet();
Session["Edit"] = EditDataSet;
Session["Valid"] = ValidDataset;
EditAdapter.Fill(EditDataSet);
ValidAdapter.Fill(ValidDataset);
ExcelAdapter.Fill(ExcelDataSet);
con.Close();
GridView5.DataSource = ValidDataset;
GridView4.DataSource = EditDataSet;
GridView3.DataSource = ExcelDataSet;
GridView3.DataBind();
GridView4.DataBind();
GridView5.DataBind();
}
else
{
MessageLbl.Text = "Attachment file size should not be greater than 1 MB";
}
}
}
catch (Exception ex)
{
MessageLbl.Text = "Error occurred while uploading file. " + ex.Message;
}
}
else
{
MessageLbl.Text = "";
}
}
&#13;
答案 0 :(得分:0)
假设数据表中的列相同,您可以使用其索引标识该行。您可以将行的值复制到第二个表中的新行。您不能只添加属于dtEdit的行,除非您使用.ItemArray,否则将抛出错误。
DataSet EditT = new DataSet();
DataSet ValidT = new DataSet();
DataRow row;
DataTable dtEdit = EditT.Tables[0];
DataTable dtValid = ValidT.Tables[0];
//Assuming columns match exactly;
int rowIndex = 0; //Find the index of the row that is clicked.
row = dtEdit.Rows[rowIndex];
dtValid.Rows.Add(row.ItemArray); //NB: Must use .ItemArray as row belongs to dtEdit.
dtEdit.Rows[rowIndex].Delete();