为什么显示下拉列表需要在DataGridView中单击两次?

时间:2010-12-25 08:58:58

标签: c# .net winforms datagridview datagridviewcombobox

我在DataGridView控件中使用了一个下拉列表,但问题是我第一次单击下拉列表时,需要两次单击才能下拉列表并显示,但之后它工作正常。

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

1 个答案:

答案 0 :(得分:4)

这是预期的行为。第一次点击是将焦点设置到组合框所必需的。一旦控件具有焦点,第二次单击就会显示下拉列表。

这会回答你的问题吗?或者您是否觉得有必要覆盖默认行为?在回答“是”之前,请考虑键盘用户以及使用箭头键在DataGridView中从单元到单元格导航的用户。

如果答案仍然是肯定的,请参阅my answer此相关问题。基本上,您需要确保DataGridView控件的EditMode property设置为“EditOnEnter”,然后虚拟“按下”{{3中的 F4 键用于下拉组合框的事件处理程序。


暂且不说 你的代码中应该 块为空Catch块!解决了这个问题。