So, I have 2 forms. The main on has a TextBox
and if I press F1
it will open a new form with a DataGridView
depending on the inserted values on the TextBox
. After double clicking in that row from the second form it will go again for the main form and fill the TextBox
with the selected row.
Then, in the main form, I have a Validating event on the TextBox
and it will be able to depending on that value show it in the Main form DataGridView
.
Unfortunately it doesn't work and I think the issue comes from the CauseValidation
from other components. I disabled it by using for example: dataGridView1.CauseValidation = false;
, but still the same.
This is the code on the TextBox
event:
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);
}
}
What should I do?
Instead of putting all the logic in the txtCargs_Validating
event, make a method that the 2nd form calls when its closed. You can do this by passing the instance of the 1st form to the 2nd, eg,
public class SecondForm : Form {
private Form _1stForm;
public SecondForm(Form 1stForm)
{
_1stForm = 1stForm;
}
...
public SecondForm_Closing(object sender, EventArgs e)
{
_1stForm.SetTheTextBox(theRowValueSelected);
}
Calling Code from the main form:
var frm = new SecondForm(this);
frm.Show();
The better solution is to have all your Code in a Business logic layer (like a Controller in MVC, or a ModelView in MVVM) and BIND all the UI Controls to the data structures in the business logic layer.
Either way save the validating event for validating, eg:
MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
I suggest, make this event your customized function instead with bool return type. use a text_changed event on the text box, and Then call Validate function. I've tried sample Textbox_Validating event and its called at form closing.
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;
}
I think the problem here is that you want a fix to your solution, rather than solve your problem.
Here's a quick sketch of how I think this can be achieved:
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.