0

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);
            }
user3307685
  • 37
  • 1
  • 2
  • 9
  • How do you bind data in GridView? Have you tried using `dgv.DataBind()` for updating? – BblackK Aug 25 '14 at 12:45
  • 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 Aug 25 '14 at 12:57
  • there seems to be some sort of problem with the CheckBoxColumn of DGV itself: http://stackoverflow.com/questions/13338837/check-uncheck-a-checkbox-on-datagridview or maybe this may help: http://stackoverflow.com/questions/2047778/bound-datagridviewcheckboxcolumn-not-updating-binding-source – Arie Aug 25 '14 at 13:59

1 Answers1

0

Solved by adding the following code:

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

user3307685
  • 37
  • 1
  • 2
  • 9