gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:29 26 4
gpt4 key购买 nike

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

最佳答案

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

这是否回答了您的问题?或者您是否觉得需要覆盖默认行为?在回答"is"之前,请考虑键盘用户和使用箭头键在 DataGridView 中从一个单元格导航到另一个单元格的用户。

如果答案仍然是肯定的,请参阅 my answer到这个相关的问题。本质上,您需要确保 EditMode property您的 DataGridView 控件设置为“EditOnEnter”,然后实际上“按下”EditingControlShowing 中的 F4 键。下拉组合框的事件处理程序。


顺便说一句:您的代码中不应该有空的Catch block !解决这个问题。

关于c# - 为什么显示下拉列表需要在 DataGridView 中单击两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4529809/

26 4 0
文章推荐: css - 显示网格

行与网格

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com