Populating controls from a ListView

I have a ListView control that is populated with values from a DataSet. I want to (upon selecting a row in the ListView control) populate a series of controls on the same form (text boxes, combo boxes, etc.). In VB6, I would use the ItemData property to snag the ID and use that as a key to find the corresponding record.

I don't see a way to associate an ID with each row in the ListView. If I can do this, I can then get the ID and get the corresponding record from the DataSet (using DataRow = DataSet.Tables["Table_Name"].Rows.Find(Table_ID) ).

Any help in this area would be great.

Thanks!!




Answer this question

Populating controls from a ListView

  • bizbuz

    This is how I'm populating my listview:

    private void populateVendors()

    {

        DataTable dtVendor = mdsVendor.Tables[0];

        lstVendor.Clear();

        this.lstVendor.Columns.Add("Number",200,HorizontalAlignment.Left);

        this.lstVendor.Columns.Add("Name",300,HorizontalAlignment.Left);

        this.lstVendor.Columns.Add("State",64,HorizontalAlignment.Left);

        for (int i=0; i< dtVendor.Rows.Count; i++)

        {

             DataRow drVendor = dtVendor.RowsIdea;

            //define the listview item

            ListViewItem lviVendor = new ListViewItem();

            lviVendor.Text = drVendor["VNDR_NBR"].ToString();

            // add the listview item

            lstVendor.Items.Add (lviVendor);

            lviVendor.SubItems.Add (drVendor["VNDR_NM_TXT"].ToString());

            lviVendor.SubItems.Add (drVendor["STE_ADR"].ToString());

         }

    }

    Excuse my ignorance, but based upon the above code, I don't think that I'm encapsulating the items as you said. Thanks for the feedback...



  • spike76

    Do you have objects defined that encapsulate the items you're representing in the Listview   If so, then you can plug those objects in the Tag property of the ListViewItem and then you don't need to go back to your dataset.  Or, you can simply place the DataRow from which you're retrieving data into the Tag property of the ListviewItem.



  • Jonathan Bower

    Thank you for the help Jacob. This gets me pointed in the right direction Big Smile Have a great w/e...

  • CavemanUK

    You are correct, you're not encapsulating them.  But, you can add this little line:


    lviVendor.Tag = drVendor;
     


    Then, when the user selects a listviewitem, you can pull the row directly from the Tag property of the ListviewItem.

  • Populating controls from a ListView