Solved

Adding values to a ListBox

Posted on 2007-11-26
14
175 Views
Last Modified: 2010-04-15
Hey I have a ListBox whose values should be added and displayed.I could see the values being added on the code behind but the values are not being displayed instead its method is being called.For example if a particular user registers for 6 products then the names of the products should be displayed in the list box.I used List interface generic collection to populate th ID and Name of the product....but only should be displayed.....It is a winforms Application
What can I do ?
Help is appreciated!
0
Comment
Question by:onebite2
  • 7
  • 7
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
not sure about your question. The listbox accept any object as a item, the text show for each object is it ToString() implementation. So, add the objects to the listbox, and override the ToString() method for each object class if you want to show anything different
0
 

Author Comment

by:onebite2
Comment Utility
Hey Jaime ,

I did the same way to append here goes my code,At the last you can see
  productList.Add(product); which adds the products when I debug the, product holds=the value of the method "products registered"
 I can see all the names correctly in the code behind but the listbox display shows only the method names instead of the Name....Can you tell me what was my mistake?????I have been trying to figure out since long time........

Form1.cs
private List<UserData.ProductsRegistered> proreg;

        private void ProductsRegistered(string UserName, string productID,string Name)
        {
            proreg =userdata.GetRegisteredProducts(UserName, productID,Name);

                     
            listBox1.DataSource = proreg;
            listBox1.DisplayMember =Name;
                                 
        }

UserData.cs:

public class ProductsRegistered
    {
        public string ProductID ;
        public string Name;
    }
 
    public List<ProductsRegistered> GetRegisteredProducts(string UserName, string ProductID,string Name)
    {
 
        List<ProductsRegistered> productList = new List<ProductsRegistered>();
 
        string commandText =
            string.Format("SELECT DISTINCT a.UserID,a.ProductID,b.[Name] as Name" +
            "  FROM myacc_userregisteredProducts a " +
            "    INNER JOIN InstallationProduct b ON a.ProductID=b.[ID]" +
            "    INNER JOIN myacc_users c ON a.UserID=c.[ID]" +
            "  WHERE c.UserName ='{0}' ", UserName);
               
       // string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
       using (SqlConnection connection = new SqlConnection("server=Intranet9; database=svc; uid=sa; pwd=!galore"))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ProductsRegistered product = new ProductsRegistered();

                        // product.ProductID = int.Parse(reader["ProductID"].ToString());
                        product.ProductID = reader["ProductID"].ToString();
                        product.Name = reader["Name"].ToString();

                        productList.Add(product);
                                               
                   }
                    return productList;
                }
            }
        }
 
    }

Thanks!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
>>...but the listbox display shows only the method names instead of the Name
Which method names?
0
 

Author Comment

by:onebite2
Comment Utility
It is showing :MyAccountConsole.UserData.ProductsRegistered+ProductsRegistered in the lsit box instead of product Name...
and the value product = {MyAccountConsole.UserData.ProductsRegistered}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
as far as I know, you have to define ValueMember too.
Have a look to this tutorial:
http://www.codeproject.com/csharp/ScoMListControlBinding.asp
0
 

Author Comment

by:onebite2
Comment Utility
Okay I did declare value memeber too but still its the same ..............do u have any other method to solve this ??I will implement ur code if u have one?


Thanks!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Non-databinding approach:


public class ProductsRegistered

{

        public string ProductID ;

        public string Name;
 

        public override string ToString()

        {

                return Name;

        }

}
 

private void ProductsRegistered(string UserName, string productID,string Name)

{

            proreg = userdata.GetRegisteredProducts(UserName, productID,Name);

            listBox1.Items.Clear();

            listBox1.Items.AddRange(proreg.ToArray());

}

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:onebite2
Comment Utility
Thanks a lot!This worked out for me by adding the ,but cna you tell me the reason behind this in a detailed way....

 public override string ToString()
        {
                return Name;
        }
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Listbox accept objects as items, a I stated earlier. Any kind of object, even distinct class for every item.
Now, the listbox has to show some text for every object it contains, but what to show?
Every .net object has a default or overriden ToString() method. The listbox will call it for every item. Just override the ToString() method to show what you want.
0
 

Author Comment

by:onebite2
Comment Utility
Thanks a lot!!!!!
0
 

Author Comment

by:onebite2
Comment Utility
I have one more question can we override the TOstring() method tos how any number of values or just one parameter?????

I mean If I want to show even the serial number of that product and its complete license information.....what best method can I do to display everything related to taht particular productname?

Thanks!in advance.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
you show anything like:
public override string ToString()
        {
                return ID + " - " + Name;
        }
0
 

Author Comment

by:onebite2
Comment Utility
Thanks!
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
I will preciate if you close this question, I have hundreds of pending questions.
Cheers,
Jaime.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now