所以,我有2个表格。主要内容为TextBox
,如果按F1
,则会根据DataGridView
上的插入值打开一个TextBox
的新表单。双击第二个表单中的该行后,它将再次显示主表单并填充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 :(得分: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,或MVVM中的ModelView)中,并将 BIND 所有UI控件放到业务中的数据结构中逻辑层。
无论哪种方式保存验证事件以进行验证,例如:
MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
答案 1 :(得分:0)
我建议,使用bool返回类型将此事件设为自定义函数。在文本框中使用text_changed事件,然后调用Validate函数。我已经尝试过示例Textbox_Validating事件,并在表单关闭时调用它。
答案 2 :(得分: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;
}
答案 3 :(得分: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();
}
}