Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Populating a Listbox from ArrayList from Struct from MSSQL

Posted on 2005-03-08
4
Medium Priority
?
448 Views
Last Modified: 2011-09-20
I trying to populate a Windows Form Listbox from data stored in an ArrayList.  The array list consists of a struct in which the data comes from MSSQL.

I am able to get the data from MSSQL.
I am able to populate the Struct and put the data in the ArrayList.
My problem is getting the data from the ArrayList and into the Listbox.

My Struct us called   udtCustomers   and has the following members:
  PrimaryKey
  CustomerName
  CustomerType

My Arraylist is called    alCustomerItemsDB

My ListBox is called   lboxCustomers

After filling the arraylist from MSSQL  I try to populate the Listbox using the PopulateCustomerLookup method below:
NOTE: I would like to store the  PrimaryKey  in the  ValueMember  property and the  CustomerName  in the DisplayMember  property of the Listbox.

private void PopulateCustomerLookup()
{
  // Shutdown the painting of the ListBox as items are added.
  this.lboxCustomers.BeginUpdate();

  // loop through each radio button in the alProductButtons array list.
  foreach(udtCustomers udtCust in this.alCustomerItemsDB)
 {
     this.lboxCustomers.Items.Add(udtCust.CustomerName);
  }

  // Allow the ListBox to repaint and display the new items.
  this.lboxCustomers.EndUpdate();
}
0
Comment
Question by:wenyonk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 total points
ID: 13485774
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        struct udtCustomers
        {
            public udtCustomers(string key, string name)
            {
                sPrimaryKey = key;
                sCustomerName = name;
            }

            private string sPrimaryKey;
            private string sCustomerName;

            public string PrimaryKey
            {
                get
                {
                    return sPrimaryKey;
                }
            }

            public string CustomerName
            {
                get
                {
                    return sCustomerName;
                }
            }

        }

        private System.Windows.Forms.ListBox listBox1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            //
            // listBox1
            //
            this.listBox1.Location = new System.Drawing.Point(32, 64);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(236, 134);
            this.listBox1.TabIndex = 0;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.listBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            ArrayList customers = new ArrayList();
            customers.Add(new udtCustomers("1", "A"));
            customers.Add(new udtCustomers("2", "B"));
            customers.Add(new udtCustomers("3", "C"));

            listBox1.DataSource = customers;
            listBox1.DisplayMember = "CustomerName";
            listBox1.ValueMember = "PrimaryKey";

        }
    }
}
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13485868
One suggestion, since ArrayList.Items collection stores objects (reference types), and structures are value types, I would change it from Structure to Class.  The terms for storing/retrieving value types is called boxing/unboxing, and is a very expensive operation, especially within For loops.

this.lboxCustomers.DataSource = this.alCustomerItemsDb;
this.lboxCustomers.DisplayMember = "CustomerName";
this.lboxCustomers.ValueMember =  "PrimaryKey";

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13486154
Snoozing and losing to a master :)

Bob
0
 

Author Comment

by:wenyonk
ID: 13486250
Thank you!
Below is My Final Code:

// Shutdown the painting of the ListBox as items are added.
this.lboxCustomers.BeginUpdate();
// Populates the list box using alCustomerItemsDB DataSource.
this.lboxCustomers.DataSource = this.alCustomerItemsDB;
// Set the DisplayMember property (this is what is displayed in the listbox).
this.lboxCustomers.DisplayMember = "CustomerName";
// Set the ValueMember property.
this.lboxCustomers.ValueMember = "PkCustomerIR";
// Allow the ListBox to repaint and display the new items.
this.lboxCustomers.EndUpdate();

The LearnedOne: Sorry, AlexFM was the first to respond :(


Note:  I am sorry I should have been more clear.  

I have the following line of code in my this customer lookup form:
  private NameSpace.caDefinedDataTypes.structCustomers.strctCustomers udtCustomers = new NameSpace.caDefinedDataTypes.structCustomers.strctCustomers();

I do have a class as described below, I called it a struct but it is a user defined data type with in a class (See below).
...
public class structCustomers
{
  public structCustomers()
{
  //
  // TODO: Add constructor logic here
  //
}

   public struct strctCustomers
  {
     private object objPkCustomerIR;
     private string strCustomerName;
     private object objFkCustomerTypeIR;

  }
}
...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question