tags:

views:

170

answers:

4

hi ,

i got a listview in the windows form.When form loads lisview loading with personel objects.I wanna do when some user dclick listview's , gets the personel object from the listview's selecteditem property and open a new form and transfer this object to newly opened form.

here is my codes for loading personel objects to listview. ;

   public static void GetAll(ListView list)
    {
        list.Items.Clear();
        using (FirebirdEntityz context = new FirebirdEntityz())
        {
            ObjectQuery<PERSONEL> query = context.PERSONEL;
            foreach (var item in query)
            {
                var mylist = new ListViewItem { Text = item.NAME };
                mylist.SubItems.Add(item.SURNAME);
                mylist.Tag = item;
                list.Items.Add(mylist);
            }
        }


    }

   private void Form1_Load(object sender, EventArgs e)
    {

        GetAll(listView1);
    }

//THIS IS MY PERSONEL OBJECT FOR TRANSFER

   private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        PERSONEL personel = (PERSONEL)listView1.SelectedItems[0].Tag;
    }
+3  A: 

You could probably just add a public PERSONEL property to the form, which you would then set in your SelectedIndexChanged event handler. Then any code that has access to your selector form could access your custom selected PERSONEL property.

Factor Mystic
A: 

You should be able to set the display member of the listview control. Berfore you enter the for loop do something like:

list.DisplayMember = "Name"

Then bind the object.

list.DataSource = query.ToList()

The selected item will give you the object you've binded...

MessageBox.Show(((PERSONEL)list.SelectedItem).Name);

This was how it worked in .net 2.0. But I am sure there is probably a way to do this in 3.0 and greater...

deostroll
+1  A: 
  • One way is to have a public propery as Factor Mystic has suggested.
  • Or you could have a parametrized ctor and pass Personnel object. Although, this might create some problem with the design view of the form in Visual Studio.
P.K
+1  A: 

In the new form that will be opened, add a new property in the form class;

private PERSONNEL Personnel{get; set;}
public ShowPersonnel(PERSONNEL _personnel){
   this.Personnel = _personnel;
   //do whatever you want here
}

In the main form;

private void listView1_SelectedIndexChanged(object sender, EventArgs e){
        PERSONNEL personnel = listView1.SelectedItems[0].Tag as PERSONNEL;
        Form2 form2 = new Form2();
        form2.ShowPersonnel(personnel);
        form2.Show();

}

May include typos. Change PERSONNEL to PERSONEL.

ercu