klanc
klanc

Reputation: 241

Transferring objects from one Winform to antoher C#

I have a quite interesting problem...

I have one Form on which I have a datagridview. From this datagrid I select an object (in my case a medicine ) and when I clik on the button "Create reciept" I'm calling my other Form called "New Recipet" and it sticks to that form datagrid

The problem is now that I want to transfer more objects in one "session" like more items on one Receipt Form.

Here is the code on FORM1

private void btnIzdavanjeRacuna_Click(object sender, EventArgs e)
{
   hzzoLijekovi selektiraniLijek = hzzoLijekoviBindingSource.Current as hzzoLijekovi;
   if (selektiraniLijek != null)
   {
      using (var db = new appotekaDBEntities())
      {
         var lijekApoteka = (from l in db.lijekovi
                             where l.serijskiBroj == selektiraniLijek.serijskiBroj
                             select l).SingleOrDefault();

         if (lijekApoteka == null)
         {
            MessageBox.Show("Lijek ne postoji u bazi", "Upozorenje");
         }
         else if (lijekApoteka.kolicina == 0)
         {
            MessageBox.Show("Lijek ne postoji trenutno na zalihi", "Upozorenje");
         }
         else
         {
            FormRacuniNovi noviRacunForma = new FormRacuniNovi(lijekApoteka);
            noviRacunForma.Show();
         }
      }
   }
}

Sorry, its on Croatian language , but you should understand the logic. So... everything works for on item or object

On the other FORM" I have

private lijekovi lijekNaRacun;
public FormRacuniNovi (lijekovi lnr)
{
   InitializeComponent();
   lijekNaRacun = lnr;
}

private void FormRacuniNovi_Load(object sender, EventArgs e)
{
   lijekoviBindingSource.DataSource = lijekNaRacun;
}        

I think I should probably transfer a binding list but lets say I want to transfer one by one item because otherwise its to complicated (I have a few other queries before I get to the datagrid I want to transfer on FORM1)

How should I solve this problem?

On the second FORM , or the form on which I want to have multiple items in the datagrid I should probably have a bindinglist, but then I have a conversion problem when transfering a .SIngleOrDefault() object...

Upvotes: 1

Views: 104

Answers (1)

TC Alper Tokcan
TC Alper Tokcan

Reputation: 369

I think this must do ;

Change New Recipet form's constructor like this :

Form1 frm;
public New_Recipet(Form1 sender)
{
    frm= sender;
}

Create a function on Form1.cs :

public void CreateRec(string argThatYouNeed,string anOtherArgThatYouNeed)
{
    //Do Whatever You Want With Args
}

and on submit button in Recipet form :

frm.CreateRec(arg1,arg2);

Upvotes: 0

Related Questions