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

x
?
Solved

Adding values to a ListBox

Posted on 2007-11-26
14
Medium Priority
?
181 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
[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
  • 7
  • 7
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20351107
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
ID: 20351334
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
ID: 20351370
>>...but the listbox display shows only the method names instead of the Name
Which method names?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:onebite2
ID: 20351564
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
ID: 20351667
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
ID: 20351735
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
ID: 20351847
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
 

Author Comment

by:onebite2
ID: 20351956
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
ID: 20352061
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
ID: 20352110
Thanks a lot!!!!!
0
 

Author Comment

by:onebite2
ID: 20352157
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
ID: 20352203
you show anything like:
public override string ToString()
        {
                return ID + " - " + Name;
        }
0
 

Author Comment

by:onebite2
ID: 20352320
Thanks!
0
 
LVL 55

Accepted Solution

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

661 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