Updating information in a datagrid by using a button

  • Hi,

    Another C# question here. What I am trying to do is update information within a datagrid that could contain multiple records and multiple edits when clicking an Update button.

    When I run my code I get no errors, but also no action happens either. Can you help me understand what I am missing here?

    SqlConnection myConnection = new SqlConnection(Test_Utility.Properties.Settings.Default.ConnectionString);

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Assignment", myConnection);

    mySqlDataAdapter.UpdateCommand = new SqlCommand("Update Assignment SET Active='0' WHERE ID='99999'", myConnection);

    SqlCommandBuilder cb = new SqlCommandBuilder(mySqlDataAdapter);

    cb.GetUpdateCommand();

    DataSet ds = new DataSet();

    mySqlDataAdapter.Fill(ds);

    dataGridView1.DataSource = ds;

    mySqlDataAdapter.Update(ds);

    Thanks in advance!

  • Hi,

    I am still trying to figure this one out. What I want to do is update all records in a datagrid when clicking an update button. I have been able to get it to update the current row, but what do I need to do from here to get it to update all records?

    Here is my latest code:

    SqlConnection updConnection = new SqlConnection(Test_Utility.Properties.Settings.Default.ConnectionString);

    string updSqlStmt = "UPDATE Assignment SET Active= '1', StatusType='9999', EndDt =@EndDt WHERE ID=@ID";

    try

    {

    updConnection.Open();

    SqlCommand updCmd = new SqlCommand(updSqlStmt, updConnection);

    updCmd.Parameters.AddWithValue("@ID", dataGridView1.CurrentRow.Cells["ID"].Value);

    updCmd.Parameters.AddWithValue("@EndDt", dataGridView1.CurrentRow.Cells["EndDt"].Value);

    updCmd.CommandType = CommandType.Text;

    updCmd.ExecuteNonQuery();

    txtProgress.Text = "Update completed.";

    AssignmentTableAdapter.FillID(ds_Assignment.tAssignment, Convert.ToInt32(txtID.Text));

    }

    finally

    {

    updConnection.Close();

    updConnection.Dispose();

    }

  • Hello,

    I have implemented a form with a grid that can be used to edit data in a variety of different tables. Then the update button calls a generic method that receives the dataset that was bound to the grid. It gets the table name from the dataset and uses a SQLCommandBuilder object to create the update (and all the other commands) from a simple select of all fields.

    Here is the code:

    public void UpdateDataGridList(DataSet gridDataSet)

    {

    // File the updates from the grid view form...

    SqlConnection connection = new SqlConnection(ConnectString);

    SqlDataAdapter tempDataAdapter;

    SqlCommandBuilder bldr; // needs to build to get update and other cmds.

    string tableName = gridDataSet.Tables[0].TableName;

    tempDataAdapter = new SqlDataAdapter("Select * from " + tableName, connection);

    bldr = new SqlCommandBuilder(tempDataAdapter);

    try

    {

    tempDataAdapter.Update(gridDataSet.Tables[tableName]);

    }

    catch (DBConcurrencyException ex)

    {

    System.Windows.Forms.MessageBox.Show("Someone else updated this data.");

    }

    }

    This updates any changes I made anywhere in the data set.

    Of course you need to replace ConnectString with yours and you may need to close the connection.

    Does that help?

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply