2017-02-01 6 views
2

だから、C#でのDataGridViewにダブルクリックした後、私は自分のアプリケーションが何をしたいのかを説明しようとします。 TextBoxに検索したいものを挿入し、F1をクリックして、別のDataGridViewに表示される2番目のフォームを開きます。重複フォーム

2)第2フォームDataGridViewをダブルクリックし、その列の値がメインフォームのTextBoxに表示されます。

3)その後、TextBoxが入力され、その値に応じてメインフォームDataGridViewにその値が挿入されます。ここ

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

     } 

問題:私は校長形態の第2のフォームを呼び出すとき

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

これは、次のとおりです。私はこれでダブルクリックイベントを持っているDataGridView2番目の形式では

frm.Show();を使用すると、新しいfrmPrincipalフォームが開きますが、すでに1つあります。私がfrm.Show();にコメントすると、コードは実行されませんが、エラーは表示されません。基本的にその値はTextBoxには表示されません。

どうすればよいですか?

+0

に基づいて私の答えを更新しました。 frm変数を静的変数としてクラスに入れます。最初にnullの場合のみ作成してください – Mangist

答えて

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

私がそのメソッドを使用すると、frmPrincipalが閉じられ、保持したいと思います。それは決してありませんか? – user6363145

+0

frmPrincipalがメインフォームの場合、その参照をDataGridView1があるポップアップフォームに渡す必要があります。したがって、公開MyPopupForm(frmPrincipal frm){... this.frm = frm; }そうすれば、いつでもメインフォームにアクセスできます。私はそれがあなたのメインフォームだと仮定しています。関連するすべてのコード(すべてのフォームから)を投稿しないと、あなたが何をしているのかわからない – Mangist

+0

私のポストを更新しました – user6363145

関連する問題