gridview一次更新所有修改的行
private bool tableCopied = false;//判断修改状态
private DataTable originalDataTable;//保存检索数据临时表
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//拷贝检索的数据
if (e.Row.RowType == DataControlRowType.DataRow)
if (!tableCopied)
{
originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
ViewState["originalValuesDataTable"] = originalDataTable;
tableCopied = true;
}
}
//更新所有
protected void update_Click(object sender, EventArgs e)
{
string daily_no = ViewState["daily_no"].ToString();
ArrayList SqlList = new ArrayList();
originalDataTable = (DataTable)ViewState["originalValuesDataTable"];
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
if (IsRowModified(row))
{
//相关sql操作代码
}
}
tableCopied = false;
//更新代码
bind();//绑定
}
//行是否修改
protected bool IsRowModified(GridViewRow row)
{
int sysid = int.Parse(((Label)row.Cells[0].FindControl("Lab_sysno")).Text);
string line = ((DropDownList)row.Cells[0].FindControl("DropDownLine")).SelectedItem.Value;
string prod_no = ((TextBox)row.Cells[0].FindControl("txt_prod_no")).Text;
string start_time = ViewState["producedate"].ToString() + " " + ((TextBox)row.Cells[0].FindControl("txt_start_time")).Text;
string end_time = ViewState["producedate"].ToString() + " " + ((TextBox)row.Cells[0].FindControl("txt_end_time")).Text;
string abn_id = ((TextBox)row.Cells[0].FindControl("txt_abn_id")).Text;
string abnremark = ((TextBox)row.Cells[0].FindControl("txt_abnremark")).Text;
decimal abn_dt = DateDiff(Convert.ToDateTime(start_time), Convert.ToDateTime(end_time));
decimal persons = decimal.Parse(((TextBox)row.Cells[0].FindControl("txt_abn_dt_persons")).Text);
DataRow row1 = originalDataTable.Select(String.Format("sysno = {0}", sysid))[0];
if (!line.Equals(row1["line"].ToString())) { return true; }
if (!prod_no.Equals(row1["prod_no"].ToString())) { return true; }
if (!start_time.Equals(row1["start_time"].ToString())) { return true; }
if (!end_time.Equals(row1["end_time"].ToString())) { return true; }
if (!abn_id.Equals(row1["abn_id"].ToString())) { return true; }
if (!abnremark.Equals(row1["abnremark"].ToString())) { return true; }
if (!abn_dt.Equals(decimal.Parse(row1["abn_dt"].ToString()))) { return true; }
if (!persons.Equals(decimal.Parse(row1["persons"].ToString()))) { return true; }
return false;
}