Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding values to a ListBox

Posted on 2007-11-26
14
Medium Priority
?
182 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

916 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