gpt4 book ai didi

c# - 双击进入 C# 中的 DataGridView 后出现重复的表单

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:06 25 4
gpt4 key购买 nike

所以,我将尝试解释我希望我的应用程序做什么:

1) 在主窗体中,我有一个 TextBox 和一个 DataGridView。我将在 TextBox 中插入我要搜索的内容,然后单击 F1 打开第二个表单,该表单将显示在另一个 DataGridView 中。

2) 我将双击进入第二个窗体 DataGridView,该列值将显示在主窗体的 TextBox 中。

3) 之后,TextBox 被填充,并根据该值插入到主窗体 DataGridView 中,该值详细说明。

Second Form 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 中。

我该怎么办?

最佳答案

我已经根据您的新修改更新了我的答案

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

关于c# - 双击进入 C# 中的 DataGridView 后出现重复的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981821/

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