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