0

When I check a checkbox in dataGridView, the checkbox that I've checked is automatically becoming false because I am refreshing my dataGridView every second. What I want to happen is to cancel the refreshing every second when a dataGridView checkbox is checked.

Here is my code:

private void UpdateVisitors_Load(object sender, EventArgs e)
{
    //Realtime refresh
    refresher.Interval = (1 * 1000); // 10 secs
    refresher.Tick += new EventHandler(refresh);
    refresher.Start();
}

private void refresh(object sender, EventArgs e)
{
   refreshLocal();
}

UPDATE Here is my refreshLocal code that I've been using to refresh my dataGridView

 void refreshLocal()
    {
        dgvLocal.Rows.Clear();
        connection.Close();
        connection.Open();

        SqlCommand cmd = connection.CreateCommand();
        cmd.CommandText = "Select * from tbl_Registration ORDER BY [ID] DESC;";
        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            myID = dr["ID"].ToString();
            category = dr["Category"].ToString();
            repname = dr["Representative"].ToString();
            if (dr["City/Province"].ToString() == "")
            {
                city = dr["Province"].ToString();
            }
            else
            {
                city = dr["City/Province"].ToString();
            }
            pax = dr["Pax"].ToString();
            male = dr["Male"].ToString();
            female = dr["Female"].ToString();
            students = dr["Students"].ToString();
            ar = dr["AR Users"].ToString();
            date = dr["Date & Time Added"].ToString();
            dgvLocal.Rows.Add(false, myID, category, repname, city, pax,students, ar, date);
        }
        connection.Close();
    }
6
  • 1
    Maybe place your "datagrid stopping checkbox" outside of you datagrid? Commented Sep 28, 2018 at 12:54
  • Possible duplicate of How to check if dataGridView checkBox is checked? Commented Sep 28, 2018 at 12:57
  • stackoverflow.com/questions/18439758/… Commented Sep 28, 2018 at 12:58
  • 1
    I dont know what do you do in refreshLocal , but maybe you can set true to source data before binding if related record is checked Commented Sep 28, 2018 at 12:58
  • 1
    Most of that code is not needed. Using databinding, there is no need to tediously populate each row and using a persistent DataAdapter, you can set it up to do actual refreshes of the data Commented Sep 28, 2018 at 14:21

1 Answer 1

1

You need to bind to the event CellValueChanged of your DataGridView and inside the handler, stop your timer depending on the value of your checkbox.

private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
    if (dataGridView.Columns[e.ColumnIndex].Name == "MyCheckBoxCellName" && dataGridView.Columns[e.ColumnIndex].Value) {
        // Disable your Timer
        refresher.Enabled = false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.