简体   繁体   中英

C# Check if any checkbox in datagridview is checked

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();
    }

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM