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

I m building a windows application and I want to update the checked rows of the datagridview in the database. I have tried many options but m not getting the result as i want. Following is my code that i m trying :

C#
foreach (DataGridViewRow dr in dgvPendingApprovals.Rows)
{
    int rid = int.Parse(dgvPendingApprovals[1, dr.Index].Value.ToString());

    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
    ch1 = (DataGridViewCheckBoxCell)dgvPendingApprovals.Rows[dgvPendingApprovals.CurrentRow.Index].Cells[0];
    if (ch1.Value == null)
        ch1.Value = false;
    switch (ch1.Value.ToString())
    {
        case "True":
            ch1.Value = false;
            break;
        case "False":
            cmd.CommandText = "Update ApplicantDetails set Status2 = 'Yes', ApprovedDate2 =  getdate() where Id = '" + rid + "'";
            cmd.Connection = conobj.getConn();
            conobj.openCon();
            cmd.ExecuteNonQuery();
            conobj.closeCon();
            rid = 0;
            MessageBox.Show(rid + " Updated");
            break;
    }

}
HTML



I want that for each datarow that for datarow whose checkbox.check value is true, that row should get updated. But when using this code only the row that is selected gets updated, not the one whose checkbox is checked. Please help me with a solution for this.
Posted

Remove WHERE clause from your Update Query
 
Share this answer
 
Comments
MalwareTrojan 9-Mar-13 3:05am    
is it working now?
Vreeti 11-Mar-13 0:14am    
How can i remove the where clause... I want to update the selected row ids in the table.. So there is no question of removing the where clause.....
I got the solution....... and it is working the way i want... following is the code.

C#
foreach (DataGridViewRow dr in dgvPendingApprovals.Rows)
{
    int rid = int.Parse(dgvPendingApprovals[1, dr.Index].Value.ToString());
    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
    ch1 = (DataGridViewCheckBoxCell)dr.Cells[0];
    try
    {
        switch (ch1.Value.ToString())
        {
            case "False":
                ch1.Value = false;
                break;
            case "True":
                cmd.CommandText = "Update ApplicantDetails set Status2 = 'Yes', ApprovedDate2 =  getdate() where Id = '" + rid + "'";
                cmd.Connection = conobj.getConn();
                conobj.openCon();
                cmd.ExecuteNonQuery();
                conobj.closeCon();
                rid = 0;
                break;
        }
    }
    catch { }
}
 
Share this answer
 

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