Solved

combo box question for window forms

Posted on 2004-08-17
14
188 Views
Last Modified: 2010-04-15
I'm moving an app from web to windows (cause I'm glutton for punishment) and I did something like this in the web app to fill out a dropDownList

            //Products
            foreach(int id in r.GetTableIdArray("ProductType")){            
                  ProductTypes.Items.Add(new ListItem(r.GetTableValues("ProductType", id), id.ToString()));
            }

which gives a label and a value to one item in the drop down.

I'm using a combo box in windows forms and I'm wondering how to do the same type of thing so that when I click submit I'll have the value behind the label?

right now I have this..

                  //Products
                  foreach(int id in r.GetTableIdArray("ProductType")){            
                        this.ProductTypeCB.Items.Add(r.GetTableValues("ProductType", id).ToString());
                  }

ideas and code welcomed

thanks
0
Comment
Question by:jayrod
  • 8
  • 5
14 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 400 total points
Comment Utility
How about just binding to the data directly?

ProductTypeCB.DataSource = this.m_dsMaster;
ProductTypeCB.DisplayMember = "Product.Description";
ProductTypeCB.ValueMember = "Product.ProductType";

Then SelectedValue and SelectedText will return what you need.
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
this.m_dsMaster

That a dataset?
0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
Yes it is.
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
I'm not returning datasets from this function

r.GetTableValues("ProductType", id)

The way that I usually do everything is that I will open and close connections at the beginning and end of each function. If I close the connection before returning the dataSet it seems to crap out.
0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
In ADO.Net you open your connection, fill in a dataset, close your connection.  Then use your DataSet disconnected and anytime you wish.

Something like this should work out well.
 
DataTable productTypes = r.GetProductTypes( );
ProductTypeCB.DataSource = productTypes;
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
ok thanks,

quick sub question, am I returning a dataset or a data table? I'm a little clueless on the particulars of ado.net
0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
Either.  Both will work as a DataSource.  
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Author Comment

by:jayrod
Comment Utility
ok my function is set up like this;

SqlConnection connection = null;                  
DataSet r1 = null;

string retString = "";

try {
      try {
            connection = Helper.GetConnection();
      }
      catch(Exception ex) {
            throw(ex);
      }
      SqlParameter[] parms = new SqlParameter[2];
      parms[0] = new SqlParameter("@tableName", tableName);
      parms[1] = new SqlParameter("@identityId", identityId);

      r1 = SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, "RatesValuesGet_SP", parms);
      
}
catch(Exception ex) {
      throw(ex);
}
finally {
      if(connection != null)
            connection.Dispose();
}

return r1;

the stored procedure will contain a resultset with two columns.. a column and an id..

How would I access them?
0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
Not real sure, I always use typed datasets.  Plus, not sure of what your fields being returned looks like.  Something like this.

ProductTypeCB.DataSource = r1;
ProductTypeCB.DisplayMember = "r1.ProductID";
ProductTypeCB.ValueMember = "r1.ProductName";

0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
yep... that makes sense let me try that real quick
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
welp.. can't get it to work at all.. so I'm gonna have to find a non ado solution
0
 
LVL 7

Assisted Solution

by:NipNFriar_Tuck
NipNFriar_Tuck earned 100 total points
Comment Utility
Try this...

public class MyClass {
   public int Id;
   public string Name;

   public MyClass( int Id_p, string Name_p ) {
      id = Id_p;
      name = Name_p;
   }
}

ProductTypeCB.DisplayMemeber = Id;
ProductTypeCB.ValueMember = Name;

foreach(int id in r.GetTableIdArray("ProductType")){          
    this.ProductTypeCB.Items.Add(new MyClass(id, r.GetTableValues("ProductType", id).ToString()));
}
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
Thanks nip.. But don't think that's gonna work for me either. I just think it's strange to have a dropdownlist type of object in asp.net not be present in windows applications??
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
Had to go with another round about solution...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

18 Experts available now in Live!

Get 1:1 Help Now