我在我的項目中有一個場景我有一個網格視圖和提交按鈕,它會生成動態行,並且有一個圖像按鈕Edit
它編輯這些行,並且這些行首先綁定數據表,當我們點擊保存按鈕全部反映在數據表中所做的更改保存在數據庫中沒有Update
按鈕從gridview更新datatable
問:
我如何能在以前編輯的行保存到DataTable
並在其中活動嗎?
plz幫助您的LinkButton其迫切
我在我的項目中有一個場景我有一個網格視圖和提交按鈕,它會生成動態行,並且有一個圖像按鈕Edit
它編輯這些行,並且這些行首先綁定數據表,當我們點擊保存按鈕全部反映在數據表中所做的更改保存在數據庫中沒有Update
按鈕從gridview更新datatable
問:
我如何能在以前編輯的行保存到DataTable
並在其中活動嗎?
plz幫助您的LinkButton其迫切
聲明一個數據表,爲其添加列以表示要從網格保存到數據庫表中的所有數據。 循環遍歷網格中的行,從每個要保存的單元格中獲取數據,並將其添加到數據表中的每一列。 使用該數據表對數據庫中的表執行批量插入。
DataTable dtMealTemplate = new DataTable(); dtMealTemplate.Columns.Add("MealTemplateID", Type.GetType("System.Int32")); dtMealTemplate.Columns.Add("WeekNumber", Type.GetType("System.Int32")); dtMealTemplate.Columns.Add("DayOfWeek", Type.GetType("System.String")); foreach (GridViewRow gvr in GridView.Rows) { drMT = dtMealTemplate.NewRow(); drMT["MealTemplateID"] = gvr.Cells[0].text; drMT["WeekNumber"] = gvr.Cells[1].text; drMT["DayOfWeek"] = gvr.Cells[2].Text; dtMealTemplate.Rows.Add(drMT); } public void InsertMealsTemplate(int iMealTemplateID, DataTable dtMealsData) { SqlCommand cmd = new SqlCommand(); SqlDataAdapter sa = new SqlDataAdapter(cmd); SqlCommandBuilder cmb = new SqlCommandBuilder(sa); SqlTransaction oTrans; SqlConnection oConn = new SqlConnection(GetConnectionString()); DataSet ds = new DataSet(); oConn.Open(); cmd = oConn.CreateCommand(); oTrans = oConn.BeginTransaction(); cmd.Connection = oConn; cmd.Transaction = oTrans; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM YOURTABLENAME WHERE 1 = 1 "; sa = new SqlDataAdapter(cmd); cmb = new SqlCommandBuilder(sa); sa.MissingSchemaAction = MissingSchemaAction.AddWithKey; cmd.Transaction = oTrans; sa.Fill(ds, "yourtablename"); DataRow drNew; int x = 0; foreach (DataRow dr in dtMealsData.Rows) { if (Int32.Parse(dr["MealDetailsID"].ToString()) == 0) { drNew = ds.Tables[0].NewRow(); drNew["MealTemplateID"] = dr["MealTemplateID"]; drNew["WeekNumber"] = dr["WeekNumber"]; drNew["DayOfWeek"] = dr["DayOfWeek"]; ds.Tables[0].Rows.Add(drNew); } } sa.Update(ds.Tables["yourtablename"]); oTrans.Commit(); oConn.Close(); }
放一個的CommandName和CommandArgument,並把你的代碼保存在ItemCommand()爲GridView控件應該工作。 哦,命令名稱應該像「更新」,參數可以是該行的ID。
我想這不是那麼緊迫...... – 2012-04-18 10:54:12