Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Updating information in a datagrid by using a button Expand / Collapse
Posted Friday, April 26, 2013 9:18 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 21, 2014 1:20 PM
Points: 47, Visits: 126

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);

DataSet ds = new DataSet();
dataGridView1.DataSource = ds;

Thanks in advance!
Post #1447058
Posted Tuesday, April 30, 2013 8:03 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 21, 2014 1:20 PM
Points: 47, Visits: 126

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";

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;

txtProgress.Text = "Update completed.";
AssignmentTableAdapter.FillID(ds_Assignment.tAssignment, Convert.ToInt32(txtID.Text));
Post #1448046
Posted Wednesday, May 01, 2013 8:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, April 18, 2014 7:40 AM
Points: 346, Visits: 1,065
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);
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?
Post #1448450
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse