2017-02-01 60 views
0

所以,我有2種形式。主要有一個TextBox,如果我按F1它將打開一個DataGridView的新表格,具體取決於TextBox上插入的值。從第二個窗體中雙擊該行後,它將再次顯示主窗體,並將TextBox填入所選行。使用C中的TextBox驗證事件#

然後,在主窗體中,我在TextBox上有一個驗證事件,它將能夠根據該值在主窗體DataGridView中顯示它。

不幸的是,它不起作用,我認爲這個問題來自CauseValidation從其他組件。我通過使用例如:dataGridView1.CauseValidation = false;來禁用它,但仍然是相同的。

這是對TextBox事件的代碼:

private void txtCargs_Validating(object sender, CancelEventArgs e) 
    { 
     e.Cancel = false; 
     try 
     { 
      SqlConnection con = new SqlConnection(cs.DBConnP); 
      con.Open(); 

      string querySelect = @"SELECT RTRIM(CL.Cargs) AS 'Cargs', RTRIM(S.Abvs) AS 'Abss', RTRIM(CL.Linha) AS 'Linha', RTRIM(CL.Qtd) AS 'Quantity' 
            FROM CargaCab CC (NOLOCK) 
            INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs 
            INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code 
            INNER JOIN Marks M (NOLOCK) ON S.Marks = M.Marks 
            WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.TypeEmb = 'P' 
            AND CC.TypeD = 'OCS' AND CL.Cargs = '" + txtCargs.Text.Trim() + "' ORDER BY CL.Carga, S.Marks DESC, S.Abvs"; 
      cmd = new SqlCommand(querySelect); 
      cmd.Connection = con; 

      SqlDataAdapter da = new SqlDataAdapter(cmd); 

      DataSet ds = new DataSet(); 
      da.Fill(ds, "CargaCab"); 

      dataGridView1.DataSource = ds.Tables["CargaCabee"].DefaultView; 

      dataGridView1.Columns[0].ReadOnly = true; 
      dataGridView1.Columns[1].ReadOnly = true; 
      dataGridView1.Columns[2].ReadOnly = true; 
      dataGridView1.Columns[3].ReadOnly = false; 

      con.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

我該怎麼辦?

+0

在什麼時候該代碼會失敗? –

+0

代碼不會在斷點處運行 – RekcsGaming

+1

您爲什麼要嘗試在「驗證」事件上執行此操作?據我所知,當你試圖將注意力放在某些控制上時,驗證就會觸發。你不能使用「TextChanged」事件嗎? –

回答

1

短期

而不是把所有的邏輯在txtCargs_Validating事件,使該第二形式調用它關閉時的方法。你可以通過第1形式的實例爲2,如做到這一點,

public class SecondForm : Form { 

    private Form _1stForm; 
    public SecondForm(Form 1stForm) 
    { 
    _1stForm = 1stForm; 
    } 

...

public SecondForm_Closing(object sender, EventArgs e) 
{ 
_1stForm.SetTheTextBox(theRowValueSelected); 
} 

從主窗體調用代碼:

var frm = new SecondForm(this); 
frm.Show(); 

龍術語

更好的解決方案是將所有代碼放在業務邏輯層中(如MVC中的Controller或ModelView中的MVVM)和BIND業務邏輯層中數據結構的所有UI控件。

不管怎樣保存驗證事件驗證,如:

MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
0

我建議,使這個事件您的自定義函數,而不是布爾返回類型。在文本框中使用text_changed事件,然後調用Validate函數。我已經嘗試過示例Textbox_Validating事件,並在窗體關閉時調用它。

+0

您可以更具體地使用代碼嗎? – RekcsGaming

0
private void txtCargs_TextChanged(object sender, EventArgs e) 
     { 
      if (ValidateText()) 
      { 
       //Then do this 
      } 
     } 

     private bool ValidateText() 
     { 
      bool Isvalidated = false; 

      try 
      { 
       SqlConnection con = new SqlConnection(cs.DBConnP); 
       con.Open(); 

       string querySelect = @"SELECT RTRIM(CL.Cargs) AS 'Cargs', RTRIM(S.Abvs) AS 'Abss', RTRIM(CL.Linha) AS 'Linha', RTRIM(CL.Qtd) AS 'Quantity' 
            FROM CargaCab CC (NOLOCK) 
            INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs 
            INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code 
            INNER JOIN Marks M (NOLOCK) ON S.Marks = M.Marks 
            WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.TypeEmb = 'P' 
            AND CC.TypeD = 'OCS' AND CL.Cargs = '" + txtCargs.Text.Trim() + "' ORDER BY CL.Carga, S.Marks DESC, S.Abvs"; 
       cmd = new SqlCommand(querySelect); 
       cmd.Connection = con; 

       SqlDataAdapter da = new SqlDataAdapter(cmd); 

       DataSet ds = new DataSet(); 
       da.Fill(ds, "CargaCab"); 

       dataGridView1.DataSource = ds.Tables["CargaCabee"].DefaultView; 

       dataGridView1.Columns[0].ReadOnly = true; 
       dataGridView1.Columns[1].ReadOnly = true; 
       dataGridView1.Columns[2].ReadOnly = true; 
       dataGridView1.Columns[3].ReadOnly = false; 

       con.Close(); 
       Isvalidated = true; 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       Isvalidated = false; 
      } 

      return Isvalidated; 
     } 
+0

我想投入去什麼「//那麼做到這一點」是你得到了什麼上ValidateText – RekcsGaming

+0

:) OK,然後替換此 如果(ValidateText()) 與單純{// 然後做這個 } 這個ValidateText(); –

+0

它也不起作用。我將它用於驗證事件中,並且沒有任何內容顯示到DataGridView – RekcsGaming

0

我認爲這裏的問題是,您想要解決您的解決方案,而不是解決您的問題。

這裏是如何,我認爲這是可以實現的速寫:

public class Form1 : Form 
{ 
    public TextBox textBox1 { get; set; } 

    public Button button1 { get; set; } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     var form = new Form2(); 
     form.Show(); 
     textBox1.Text = form.val; 
     //do your sql stuff here 
    } 
} 

public class Form2 : Form 
{ 
    public DataGridView datagriview1 { get; set; } 

    public string val { get; set; } 

    private void datagriview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.RowIndex > -1) 
      val = datagriview1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
     Close(); 
    } 
}