Ashok Kumar

Ashok Kumar

  • NA
  • 38
  • 45.9k

problem with update in datagridview

Jun 27 2012 6:02 AM
hi friends,
I am working on datagridview in windows from application i have created two link buttons(delete, Update) in datagridview for delete button my code is working but for update command is not working could any one give me correct code for update button. which i have done mentioned below


thanks


private void LoadData()
    {
        try
        {
              sqlAdapter = new SqlDataAdapter("SELECT *, 'Delete' AS [Delete] FROM Employees", sqlCon);
               sqlCommand = new SqlCommandBuilder(sqlAdapter);
              sqlAdapter.UpdateCommand = sqlCommand.GetUpdateCommand();
               sqlAdapter.DeleteCommand = sqlCommand.GetDeleteCommand();
     
            dataset = new DataSet();
            sqlAdapter.Fill(dataset, "empdt");
            dgvEmployee.DataSource = null;
            dgvEmployee.DataSource = dataset.Tables["empdt"];

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
  private void dgvEmployee_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 4)
                {
                 if (MessageBox.Show("Are you sure to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        int rowIndex = e.RowIndex;
                        dgvEmployee.Rows.RemoveAt(rowIndex);
                        dataset.Tables["empdt"].Rows[rowIndex].Delete();
                        sqlAdapter.Update(dataset, "empdt");
                    }
                }
            }
               else if (Task == "Update")
                {                  
                    int r = e.RowIndex;
                    dataset.Tables["empdt"].Rows[r]["eid"] = dgvEmployee.Rows[r].Cells["eid"].Value;
                    dataset.Tables["empdt"].Rows[r]["ename"] = dgvEmployee.Rows[r].Cells["ename"].Value;                    dataset.Tables["Employees"].Rows[r]["Title"] = dgvEmployee.Rows[r].Cells["Title"].Value;
                dataset.Tables["empdt"].Rows[r]["eage"] = dgvEmployee.Rows[r].Cells["eage"].Value;
                dataset.Tables["empdt"].Rows[r]["esalary"] = dgvEmployee.Rows[r].Cells["esalary"].Value;                    sqlAdapter.Update(dataset, "Employees");
                    dgvEmployee.Rows[e.RowIndex].Cells[6].Value = "Delete";
                }
            }

                
catch (Exception ex)
{



 





Answers (1)