因此,我将尝试解释我希望我的应用程序执行的操作:
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("Erro\nDetalhes: " + 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
。
我该怎么办?
答案 0 :(得分:1)
我已根据您的新修改更新了我的答案
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("Erro\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}