Question

I am using a drop-down list in DataGridView control, but the problem is that for the first time I click the drop-down, it takes two clicks to drop down the list and show, but afterwards it works fine.

 private void ViewActiveJobs_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex>=0)
            {
                jobCardId = int.Parse(ViewActiveJobs.Rows[ViewActiveJobs.CurrentCell.RowIndex].Cells["Job Card Number"].Value.ToString());
                RegNo = ViewActiveJobs.Rows[ViewActiveJobs.CurrentCell.RowIndex].Cells["Registeration Number"].Value.ToString();
                SelectedRow = e.RowIndex;                
            }
        }

        private void ViewActiveJobs_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {
                ComboBox cbox = (ComboBox)e.Control;
                cbox.SelectedIndexChanged -= new EventHandler(comboBOX_SelectedIndexChanged);
                cbox.SelectedIndexChanged += new EventHandler(comboBOX_SelectedIndexChanged);
            }
            catch(Exception)
            {
            }
        }
        private void comboBOX_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            string str = combo.SelectedIndex.ToString();
           if (combo.SelectedIndex ==1)
                pdf = new MakePDF(jobCardId,RegNo);
           if (combo.SelectedIndex == 2)
           {
               PdfJobCard = new MakePDFJobCard(jobCardId);
           }
            if (combo.SelectedIndex == 3)
           {
               if (MessageBox.Show("Are you Sure you want to Close Job Card ?", "Are you Sure",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
               {
                   cmd = new SqlCommand();
                   cmd.Connection = con;

                   cmd.Parameters.Add("@jCard", SqlDbType.VarChar).Value = jobCardId;
                   cmd.Parameters.Add("@stat", SqlDbType.VarChar).Value = "Closed";
                   cmd.CommandText = "UPDATE JobCard SET status = @stat WHERE Id = @jCard";

                   try
                   {
                       cmd.ExecuteNonQuery();
                       ViewActiveJobs.Visible = false;
                       ViewActiveJobs.AllowUserToAddRows = true;
                       ViewActiveJobs.Rows.RemoveAt(SelectedRow);
                       //ViewActiveJobs.Visible = true;
                   }
                   catch (Exception c)
                   {
                       MessageBox.Show(c.Message);
                   }
               }
           }
        }
Was it helpful?

Solution

This is the expected behavior. The first click is necessary to set focus to the combo box. The second click shows the drop-down list, once the control has the focus.

Does that answer your question? Or do you feel some need to override the default behavior? Before answering yes, consider keyboard users and those navigating from cell-to-cell in your DataGridView using the arrow keys.

If the answer is still yes, see my answer to this related question. Essentially, you need to make sure that the EditMode property of your DataGridView control is set to "EditOnEnter", and then virtually "press" the F4 key in the EditingControlShowing event handler to drop down the combo box.


As an aside: You should not have empty Catch blocks in your code! Fix that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top