private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
 {
     try {
         pnlAvailableReplacements.Controls.Clear();
         AdvancedRecomendation captureEvent = dgvResults[1, e.RowIndex].Value as AdvancedRecomendation;
         List<ReplacementEvent> availableReplacements = Advanced_Replacements.GetAvailableReplacementsByCaptureId(captureEvent.CaptureEventId,BackEndUtils.GetSqlConnection());
         for (int i = 0; i < availableReplacements.Count; i++) {
             CustomizedReplacement customizedReplacement = new CustomizedReplacement();
             customizedReplacement.Click += new EventHandler(customizedReplacement_Click);
             customizedReplacement.Location = new System.Drawing.Point(3, 3 + (136 * (i)) + 6);
             customizedReplacement.Name = availableReplacements[i].name;
             customizedReplacement.repDescription = availableReplacements[i].description;
             customizedReplacement.repName = availableReplacements[i].name;
             customizedReplacement.repReplacement = availableReplacements[i].Value;
             customizedReplacement.replacementEvent = availableReplacements[i];
             customizedReplacement.Size = new System.Drawing.Size(566, 119);
             customizedReplacement.TabIndex = i;
             customizedReplacement.Visible = true;
            // customizedReplacement.popularity = (int)SetPopularity(availableReplacements[i]);
             pnlAvailableReplacements.Controls.Add(customizedReplacement);
         }
         pnlAvailableReplacements.Focus();
     } catch (Exception ex) {
         CommonUtils.ShowError(ex.Message, ex);
     }
 }
        private void ShowSearchDetailsForm_Load(object sender, EventArgs e)
        {
            try {
                List<ReplacementEvent> availableReplacements = Advanced_Replacements.GetAvailableReplacementsByCaptureId(captureEvent.CaptureEventId, BackEndUtils.GetSqlConnection());
                for (int i = 0; i < availableReplacements.Count; i++) {
                    CustomizedReplacement customizedReplacement = new CustomizedReplacement();
                    customizedReplacement.Click += new EventHandler(customizedReplacement_Click);
                    customizedReplacement.Location = new System.Drawing.Point(3, 3 + (207 * (i)) + 6);
                    customizedReplacement.Name = availableReplacements[i].name;

                    customizedReplacement.repDescription = availableReplacements[i].description;
                    customizedReplacement.repName = availableReplacements[i].name;
                    customizedReplacement.repReplacement = availableReplacements[i].Value;
                    customizedReplacement.replacementEvent = availableReplacements[i];
                    customizedReplacement.Size = new System.Drawing.Size(790, 207);
                    customizedReplacement.TabIndex = 0;
                  //  customizedReplacement.popularity = (int)SetPopularity(availableReplacements[i]);

                    pnlReplacements.Controls.Add(customizedReplacement);
                }
            } catch (Exception ex) {
                CommonUtils.ShowError(ex.Message, ex);
            }
        }
        private void dgvResults_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try {
                pnlAvailableReplacements.Controls.Clear();
                if (e.RowIndex >=0) {
                    ComplexCaptureMatchObject complexCaptureMatchObject = dgvResults[1, e.RowIndex].Value as ComplexCaptureMatchObject;
                    List<ReplacementEvent> availableReplacements = Advanced_Replacements.GetAvailableReplacementsByCaptureId(complexCaptureMatchObject.captureEvent.CaptureEventId, BackEndUtils.GetSqlConnection());
                    int highestPopularity = -1;

                    int totalUsageCount = 1;

                    for (int j = 0; j < availableReplacements.Count; j++) {
                        totalUsageCount = totalUsageCount + availableReplacements[j].usageCount;
                    }

                    for (int i = 0; i < availableReplacements.Count; i++) {
                        CustomizedReplacement customizedReplacement = new CustomizedReplacement();
                        customizedReplacement.Click += new EventHandler(customizedReplacement_Click);
                        customizedReplacement.Location = new System.Drawing.Point(3, 3 + (176 * (i)) + 6);
                        customizedReplacement.Name = availableReplacements[i].name;
                        customizedReplacement.repDescription = availableReplacements[i].description;
                        customizedReplacement.repName = availableReplacements[i].name;
                        customizedReplacement.repReplacement = availableReplacements[i].Value;
                        customizedReplacement.replacementEvent = availableReplacements[i];
                        customizedReplacement.Size = new System.Drawing.Size(558, 170);
                        customizedReplacement.TabIndex = i;
                        customizedReplacement.Visible = true;
                        customizedReplacement.popularity = (int)(((double)availableReplacements[i].usageCount / totalUsageCount) * 20);
                        customizedReplacement.rbSelectedReplacement.CheckedChanged += new EventHandler(rbSelectedReplacement_CheckedChanged);
                        customizedReplacement.MouseClick += new MouseEventHandler(rbSelectedReplacement_CheckedChanged);
                        pnlAvailableReplacements.Controls.Add(customizedReplacement);

                        // this checks the replacement with highest popularity
                        if (customizedReplacement.popularity > highestPopularity) {
                            customizedReplacement.rbSelectedReplacement.Checked = true;
                            highestPopularity = customizedReplacement.popularity;
                            complexCaptureMatchObject.usedReplacementEvent = customizedReplacement.replacementEvent;
                        }
                    }
                    pnlAvailableReplacements.Focus();
                }
            } catch (Exception ex) {
                CommonUtils.ShowError(ex.Message, ex);
            }
        }