I am showing a database table in a datagridview . I can correctly save records from datagridview to database in sql.
Now I want to change and modify some records and save these changes to the database. How can i do this? I am using a datasource binding bound to a dataset with datatable .
private void Form1_Load(object sender, EventArgs e) { this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020); } private void btnSave_Click(object sender, EventArgs e) { string code = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper(); string currency_Name = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper(); string boolBase = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string local_per_Base = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string base_per_Local = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string insert_sql = "INSERT INTO centraldb.dbo.CPDM0020(Code,Currency_Name,Base,Local_per_Base,Base_per_Local)VALUES('" + code + "','" + currency_Name + "','" + boolBase + "','" + local_per_Base + "','" + base_per_Local + "')"; if (this.ExecuteSql(insert_sql)) { MessageBox.Show("Record Inserted Successfully."); } else { MessageBox.Show("Insert Failed"); } } public bool ExecuteSql(string command) { SqlCommand sqlCommand = new SqlCommand(command, connection); connection.Open(); sqlCommand.ExecuteNonQuery(); this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020); dataGridView1.DataSource = cpdm_dataset.CPDM0020; sqlCommand.Dispose(); connection.Close(); return true; }
I can easily save new records in the database and datagridview , but I cannot edit existing records. After pressing the save button after editing, it displays the previous value again. Please, help.
user2156513
source share