Add data to a combobox from another form

0

I have two forms, in one I have a ComboBox which in the event Load uploads data from a database sql server making a query.

To add more data I would have to modify my query, which I added a button in front of ComboBox , when I clicked it I opened a small form where all the records of a sql server table are. I want that when one is selected I add it to the combo of the first form, how can I implement it?

da.Fill(TABLA);
cmbProductos.DataSource = null;
cmbProductos.DisplayMember = "Nombre";
cmbProductos.ValueMember = "id";
cmbProductos.DataSource = TABLA;

This way I load my data in ComboBox at first, this would only work while I do not close the main form, because when it is reloaded only the query data will appear ...

So I get the fields I want to add to the other form:

string concepto;
string id;
filaSeleccionada = dgvAgregarCombo.CurrentCell.RowIndex;
concepto = dgvAgregarCombo.Rows[e.RowIndex].Cells["CONCEPTO"].Value.ToString();
id = dgvAgregarCombo.Rows[e.RowIndex].Cells["ID_CONCEPTO"].Value.ToString();
    
asked by Jose Hernandez 07.06.2018 в 18:04
source

2 answers

0

Your load event is executed every time you load that form. Since that form is already loaded when you open the other, your best option, given what you propose, is to remove the load from the event load and pass it to a method, and also, after you return from the secondary load, refill the combo .

To do this, move the code that loads the combo in the load event to a method:

Private void LlenarCombo()
{
    ...
    da.Fill(TABLA);
    cmbProductos.DataSource = null;
    cmbProductos.DisplayMember = "Nombre";
    cmbProductos.ValueMember = "id";
    cmbProductos.DataSource = TABLA;
}

And in the load of your form, replace the code that loads the combo with a call to that function.

Also, add to the return of the load in the other form, a call to this function so that it reloads the combo.

    
answered by 07.06.2018 в 18:11
0

Pass com parameter a datatable

Datatable dt = New Datatable();
//llenas el datatable
Form Frm = new MyFrm(dt);
Frm.ShowDialog();
//En el otro form tomas el Datatable
    
answered by 07.06.2018 в 18:51