0
Reply

Updating a Database using Data Grid

Ray H

Ray H

Apr 16 2013 2:44 PM
1.1k

Hi,

I am a newbie and am trying to figure out how to update data from a datagrid to a database.  I have copied exisiting code.

My two questions that I have are:

1.)  How do I put loop here to update all records in data grid????????????????????
2.)  What is the correct syntax to pass an update from data grid field to the database????? (In this case EndDt field)

  int TestID = 999999;

         DataSet ds_Assignment = new DataSet();
         dataGridView1.DataSource = ds_Assignment;
         SqlConnection myConnection = new SqlConnection(Assignment_Utility.Properties.Settings.Default.STH_D_TESTConnectionString);

         try
         {
             myConnection.Open();
         }
         catch (Exception eConn)
         {
             MessageBox.Show("error opening database connection." + eConn.ToString());
             return;
         }

         SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Assignment where TestID = " + TestID.ToString(), myConnection);
         SqlCommandBuilder cb = new SqlCommandBuilder(mySqlDataAdapter);
         cb.GetUpdateCommand();

         mySqlDataAdapter.Fill(ds_Assignment, "Assignment");

// 1.)  How do I put loop here to update all records in data grid????????????????????
// 2.)  What is the correct syntax to pass an update from data grid to the database?????

        ds_Assignment.Tables[0].Rows[0]["EndDt"] =  WHAT DO I DO HERE TO GET UPDATE FROM DATA GRID?
        ds_Assignment.Tables[0].Rows[0]["Active"] = "0";
        ds_Assignment.Tables[0].Rows[0]["StatusType"] = "REVO";
        
mySqlDataAdapter.Update(ds_Assignment, "Assignment");
       
Thank you in advance.