0
votes

I have a SQL Database that contains a bit column and whenever a checkbox is clicked I want to update the bit column field with the current value.

All works great except the DataGridView doesn't update and I have to reload it to see the changes (checked/unchecked). I tried with Refresh() and RefreshEdit() but it doesn't work.

    private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        SqlConnection db = new SqlConnection(Properties.Settings.Default.BikesConn);

        if (e.ColumnIndex == 1)
        {
            if (dgv.Rows[e.RowIndex].Cells["Selected"].Value.ToString() == "True")
            {

                SqlCommand cmd = new SqlCommand(string.Format("UPDATE {0} SET Selected='0' WHERE ID={1}", Properties.Settings.Default.Profile, dgv.SelectedRows[0].Cells[0].Value.ToString()), db);
                db.Open();
                cmd.ExecuteNonQuery();
                db.Close();
                //loadGridViewer(Properties.Settings.Default.Profile);
            }

            if (dgv.Rows[e.RowIndex].Cells["Selected"].Value.ToString() == "False")
            {

                SqlCommand cmd = new SqlCommand(string.Format("UPDATE {0} SET Selected='1' WHERE ID={1}", Properties.Settings.Default.Profile, dgv.SelectedRows[0].Cells[0].Value.ToString()), db);
                db.Open();
                cmd.ExecuteNonQuery();
                db.Close();
                //loadGridViewer(Properties.Settings.Default.Profile);
            }
1
How do you bind data in GridView? Have you tried using dgv.DataBind() for updating? - BblackK
I bind using SqlDataAdapter. The checked/unckeed value gets saved in the database but in the DataGridViewer it remains unchanged until I reload it. - user3307685
there seems to be some sort of problem with the CheckBoxColumn of DGV itself: stackoverflow.com/questions/13338837/… or maybe this may help: stackoverflow.com/questions/2047778/… - Arie

1 Answers

0
votes

Solved by adding the following code:

dgv.Rows[e.RowIndex].Cells["Selected"].Value = !(bool)dgv.Rows[e.RowIndex].Cells["Selected"].Value;