因此,我将尝试解释我希望我的应用程序做什么:
1)在主窗体中,我有一个TextBox
和一个DataGridView
。我将在TextBox
中插入要搜索的内容,然后单击F1
以打开将在另一个DataGridView
中显示的第二种形式。
2)我将双击第二个表单DataGridView
,该列值将从主表单显示在TextBox
中。
3)之后,填充该TextBox
,并根据该值,它将插入到主窗体中,DataGridView
详细说明该值。
在第二个形式DataGridView
中,我有一个双击事件:
private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
this.Hide();
frmPrincipal frm = new frmPrincipal();
frm.Show();
frm.txtCarga.Text = dr.Cells[0].Value.ToString();
frm.txtCarga.Focus();
frm.txtCarga.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show("ErronDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这是我在主形式中调用第二种形式的时候:
private void txtCarga_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
this.Hide();
frmPesquisa frmP = new frmPesquisa();
frmP.Show();
con = new SqlConnection(cs.DBConnP);
con.Open();
string querySelect = @"SELECT CL.Cargs
FROM CargsCab CC (NOLOCK)
INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs
INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code
WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.Type = 'P'
AND CC.TypeB = 'OCS' AND CL.Cargs LIKE '%" + txtCargs.Text + "%' GROUP BY CL.Cargs ORDER BY CL.Cargs DESC";
cmd = new SqlCommand(querySelect);
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@Cargs", SqlDbType.NChar, 20, "CL.Cargs"));
cmd.Parameters["@Cargs"].Value = txtCargs.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "CargsCab");
frmP.dataGridView1.DataSource = ds.Tables["CargsCab"].DefaultView;
txtCarga.SelectAll();
con.Close();
}
}
这里的问题是,如果我使用该frm.Show();
它将打开一个新的 frmPrincipal 表单,但我已经有一个。如果我注释frm.Show();
则代码将不会执行,但不会显示错误。基本上该值不会显示在TextBox
中。
我该怎么办?
我已经根据您的新编辑更新了我的答案
public class frmPrincipal
{
// ....
// rest of your form Code
private void txtCarga_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
this.Hide();
frmPesquisa frmP = new frmPesquisa(this); // Pass a reference to this form
frmP.Show();
con = new SqlConnection(cs.DBConnP);
con.Open();
string querySelect = @"SELECT CL.Cargs
FROM CargsCab CC (NOLOCK)
INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs
INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code
WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.Type = 'P'
AND CC.TypeB = 'OCS' AND CL.Cargs LIKE '%" + txtCargs.Text + "%' GROUP BY CL.Cargs ORDER BY CL.Cargs DESC";
cmd = new SqlCommand(querySelect);
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@Cargs", SqlDbType.NChar, 20, "CL.Cargs"));
cmd.Parameters["@Cargs"].Value = txtCargs.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "CargsCab");
frmP.dataGridView1.DataSource = ds.Tables["CargsCab"].DefaultView;
txtCarga.SelectAll();
con.Close();
}
}
}
public class frmPesquisa
{
private frmPrincipal frmP;
public frmPesquisa()
{
InitializeComponent(); // Default constructor
}
public frmPesquisa(frmPrincipal frmP) : this()
{
this.frmP = frmP;
}
private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
this.Hide();
// If we have a reference to the main form, then show it
// and set the txtCarga text
if (this.frmP != null && !this.frmP.IsDiposed)
{
frmP.Show();
frmP.txtCarga.Text = dr.Cells[0].Value.ToString();
frmP.txtCarga.Focus();
frmP.txtCarga.SelectAll();
}
}
catch (Exception ex)
{
MessageBox.Show("ErronDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}