Click here to Skip to main content
15,887,952 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to edit,update,delete inside the datagridview control using windows application C#..

Any help would be appreciated...
Posted

MSIL
SqlConnection myConnection = new SqlConnection();
//now you can set ConnectionString property for myConnection
myConnection.ConnectionString = ConfigurationSettings.AppSettings["myConnectionString"];
try
{
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (Convert.ToInt32(dataGridView1.Rows[i].Cells["select"].Value) == 1)
        {
            if (dataGridView1.Rows.Count != null)
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    DialogResult result = MessageBox.Show("Are you sure you want to proceed in Updation ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {

                        string aa = dataGridView1.Rows[i].Cells["id"].Value.ToString();
                        string sms_policy = dataGridView1.Rows[i].Cells["smspolicyDataGridViewTextBoxColumn"].Value.ToString();
                        string income = dataGridView1.Rows[i].Cells["incomeDataGridViewTextBoxColumn"].Value.ToString();
                        string ratio = dataGridView1.Rows[i].Cells["ratioDataGridViewTextBoxColumn"].Value.ToString();
                        string payment = dataGridView1.Rows[i].Cells["paymentDataGridViewTextBoxColumn"].Value.ToString();
                        string feature = dataGridView1.Rows[i].Cells["featureDataGridViewTextBoxColumn"].Value.ToString();
                        string description = dataGridView1.Rows[i].Cells["descriptionDataGridViewTextBoxColumn"].Value.ToString();


                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandText = "update sms set  sms_policy='" + sms_policy + "',income='" + income + "',ratio='" + ratio + "',payment='" + payment + "',feature='" + feature + "',description='" + description + "'  where id='" + aa + "'";

                        cmd.Connection = myConnection;
                        myConnection.Open();
                        cmd.ExecuteNonQuery();
                        myConnection.Close();
                        Rebind();
                        MessageBox.Show("Update Record Success Folly");



                    }
                }
            }
        }
    }
}

catch (Exception a)
{
    MessageBox.Show("Server Have Technical problem Try After Some Time ");
}
 
Share this answer
 
There's a ton of info on the web, both for how to do this in production standard code, and how to use the built in functionality to write some toy code if you're doing a very simple app that doesn't need to be written properly. What have you tried ? What database are you using ?
 
Share this answer
 
Comments
neetika 2010 27-Aug-10 5:34am    
am using SQL database ..i have done all my code .i want to delete the record or update the record inside the datagridview..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900