1

我在控件中使用下拉列表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);
                   }
               }
           }
        }
4

1 回答 1

4

这是预期的行为。第一次单击对于将焦点设置到组合框是必要的。一旦控件获得焦点,第二次单击将显示下拉列表。

这是否回答你的问题?还是您觉得有必要覆盖默认行为?DataGridView在回答“是”之前,请考虑键盘用户和使用箭头键在单元之间导航的用户。

如果答案仍然是肯定的,请参阅对这个相关问题的回答。本质上,您需要确保控件的EditMode属性DataGridView设置为“EditOnEnter”,然后虚拟地“按下”事件处理程序中的F4键以下拉组合框。EditingControlShowing


顺便说一句:你的代码中应该有空Catch块!修复它。

于 2010-12-25T09:20:39.190 回答