I'm making a system for ticket sales for concerts and stuff like that. When I select the event from a comboBox, a new form is opened showing the available tickets for that show in a DataGridView; without closing the main form, which contains the billing data. When I double click the ticket chosen, the secondary form with the datagridview needs to be closed, sending certain values to the active window. I do not know how to do the last part.
I send the event from the comboBox to the new window to make the query:
Boleto b = new Boleto(comboBox8.Text);
b.Show();
Then I open the connection and load the select into the DGV
try
{
connStr = "Server=localhost;Database=quanax;Uid=root;Pwd=root;Port=3306";
conn = new MySqlConnection(connStr);
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show("Error: No se puede conectar con la base de datos");
MessageBox.Show(ex.ToString());
}
try
{
string query = "select id_boleto as 'Id', fecha as 'Fecha', evento as 'Evento', seccion as 'Sección', fila as 'Fila', asiento as 'Asiento', bloque as 'Bloque', total as 'Precio' from boletos where evento = '" + evento + "';";
mySqlDataAdapter = new MySqlDataAdapter(query, conn);
mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);
dataTable = new System.Data.DataTable();
mySqlDataAdapter.Fill(dataTable);
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataGridView1.DataSource = bindingSource;
}
catch (Exception ex)
{
MessageBox.Show("Error! Intenta de nuevo");
MessageBox.Show(ex.ToString());
}
And when double clicked I get the values that I need into variables
try
{
MySqlCommand cmdc = new MySqlCommand("select seccion, fila, asiento, bloque, total from boletos where id_boleto ="+toolStripTextBox1.Text+";", conn);
MySqlDataAdapter dataadapc = new MySqlDataAdapter(cmdc);
System.Data.DataTable datatabc = new System.Data.DataTable();
dataadapc.Fill(datatabc);
aux1 = Convert.ToString(datatabc.Rows[0][0]);
aux2 = Convert.ToString(datatabc.Rows[0][1]);
aux3 = Convert.ToString(datatabc.Rows[0][2]);
aux4 = Convert.ToString(datatabc.Rows[0][3]);
aux5 = Convert.ToString(datatabc.Rows[0][4]);
}
catch (IndexOutOfRangeException ex)
{
}
catch (Exception ex)
{
MessageBox.Show("Error: No se puede conectar con la base de datos");
MessageBox.Show(ex.Message);
}
If I needed to do this closing the main form, making an overloaded constructor whould be the solution:
try
{
this.Close();
Form1 f1 = new Form1(aux1, aux2, aux3, aux4, aux5);
f1.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.ToString());
}
But, since I'm not closing the main window, how can I send the values aux1, aux2, aux3, aux4 and aux5 to the active form when closing the secondary form? I hope I made myself clear. Thanks.
One of available way is using one data at both form and changing at second form shares changes at first form. Another way is be handling
Closed
event at first form and using second form properties. ...You should decide what do you want to do! I hope this helps.
I can suggest two ways: 1. Use "ShowDialog()". this way, you can set aux1, aux2... as properties and get the data easily from the main form:
Pass "this" as a reference and have properties in the main form to hold these aux values. So you can call this up. Overload your constructor. Make sure you save the instance of Mainform somewhere.
then you can set it up before you close:
You can declare a static list of strings and then use it. For example in your main form use this:
Then in your secondary form use them: