Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void Update_Click(object sender, EventArgs e)
      {

          for (int i = 0; i < dataGridView2.RowCount - 1; i++)
          {
              string sql = "UPDATE [SAPProduction].[ProductionData] SET " + " Jobcard= '" + dataGridView2.Rows[i].Cells["Jobcard"].Value.ToString() + "', " +
                                 " Operator = '" + dataGridView2.Rows[i].Cells["Operator"].Value.ToString() + "', " +
                                 " Supervisor = '" + dataGridView2.Rows[i].Cells["Supervisor"].Value.ToString() + "' , " +
                                 " Profilename ='" + dataGridView2.Rows[i].Cells["Profilename"].Value.ToString() + "' , " +
                                 " Process='" + dataGridView2.Rows[i].Cells["Process"].Value.ToString() + "' , " +
                                 " Date='" + dataGridView2.Rows[i].Cells["Date"].Value.ToString() + "' , " +
                                 " Remark='" + dataGridView2.Rows[i].Cells["Remark"].Value.ToString() + "' , " +
                                 " Approvedby='" + dataGridView2.Rows[i].Cells["Approvedby"].Value.ToString() + "' , " +
                                 " Division ='" + dataGridView2.Rows[i].Cells["Division"].Value.ToString() + "'  " +
                                 " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  "; // Jobcard = your desire Rows Jobcard Id

              SqlCommand cmd = new SqlCommand(sql, objConn1);
              objConn1.Open();
              cmd.ExecuteNonQuery();
              objConn1.Close();
              //MessageBox.Show("Record Update successfully...");
          }
      }



i have used above query but not working
Posted
Comments
Master Vinu 7-Dec-12 5:57am    
cmd gives above query ---- UPDATE [SAPProduction].[ProductionData] SET Jobcard= '103382', Operator = 'Chandrashekhar (N)', Supervisor = 'SAMIKSHA(DIE)' , Profilename ='AFA' , Process='Moulding ORVT ' , Date='11/29/2012 11:54:45 AM' , Remark='we' , Approvedby='gtr' , Division ='Fabric ' Where id = '12'
choudhary.sumit 7-Dec-12 6:15am    
what error u r facing?
Master Vinu 7-Dec-12 6:17am    
record not updated while click on button
Master Vinu 7-Dec-12 6:19am    
same query i am execute in sql its working but while click on button its not working ....whats the reason behind that...
Himanshu Yadav 7-Dec-12 6:53am    
its seems to complex so before executing save yr value in any local variable then pass to query this will makes you simpler and understable

1 solution

Vinayak,

the execution of your code is one direction i.e from Screen to database. If you want a two way behavior you must attach a data source to your datagridview and use that data source ( data source can be a DataTable,DataSet or DataAdaptor) to execute the backend operation thats pretty easy rather messing with the grid itself.

check the following link and try that
http://forums.devshed.com/net-development-87/update-table-in-database-through-datagridview-in-winform-350445.html[^]


SqlDataAdapter da=new SqlDataAdapter("select * from product",strcon);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource=ds.Tables[0];

//now u can save changes to back end with
da.Update(ds);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900