I have a scenario in my project I have a grid view and submit button which generates dynamic rows and there is an image button Edit which edits those rows and these rows bind with datatable first and when we click on save button all the reflect changes in datatable save in database there is no Update button
Question:
How can I save the previously edited rows into the DataTable and in which event?
Plz help its urgent
Declare a datatable, adding columns to it to represent all the data i want to save from the grid to the table in the DB.
Loop through the rows in the grid, get the data from each cell that you want to save and add it to each column in your datatable.
Use the datatable to do a bulk insert into the table in the DB.
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(); }