为什么显示下拉列表需要在 DataGridView 中单击两次?
我在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);
}
}
}
}
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);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是预期的行为。需要第一次单击才能将焦点设置到组合框。当控件获得焦点后,第二次单击将显示下拉列表。
这能回答你的问题吗?或者您是否觉得需要覆盖默认行为?在回答“是”之前,请考虑键盘用户以及使用箭头键在
DataGridView
中从一个单元格导航到另一个单元格的用户。如果答案仍然是肯定的,请参阅 我对此相关问题的回答。本质上,您需要确保 <将
DataGridView
控件的 code>EditMode 属性 设置为“EditOnEnter”,然后实际上“按下” 中的 F4 键。 href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx" rel="nofollow noreferrer">EditingControlShowing
用于下拉组合框的事件处理程序。顺便说一句:您的代码中不应该有空的
Catch
块!解决这个问题。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 yourDataGridView
control is set to "EditOnEnter", and then virtually "press" the F4 key in theEditingControlShowing
event handler to drop down the combo box.As an aside: You should not have empty
Catch
blocks in your code! Fix that.