Hello,In my windows application , I am loading a form having datagridview. First i bind this datagrid to database than i try to change particular link cell based on certain condition.

 int key = 0,CountCheck;
            string CheckSql;
            try
            {
                foreach (DataGridViewRow dgr in dataGridView1.Rows)
                {
                    key = Convert.ToInt32(dgr.Cells["id"].Value);
                    CheckSql = "Select count(catg_id) as Id From tbl_master Where deleted='n' and catg_id=" + key;
                    CountCheck= objCon.ExecuteScalar(CheckSql);

                    if (CountCheck > 0)
                    {

                        DataGridViewTextBoxCell TxtCell = new DataGridViewTextBoxCell();
                        TxtCell.Style.BackColor = Color.LimeGreen;
                        dgr.Cells["del"] = TxtCell;
                    }
                    CountCheck = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

This Code Works when i write the same on click event of a button but not on form load event. Trying to Figure Out Why This is being happen. ( In my code i want to disable delete link if condition met )