Solved

combo box question for window forms

Posted on 2004-08-17
14
196 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
[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
  • 8
  • 5
14 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 400 total points
ID: 11824214
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
ID: 11824241
this.m_dsMaster

That a dataset?
0
 
LVL 7

Expert Comment

by:psdavis
ID: 11824245
Yes it is.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 3

Author Comment

by:jayrod
ID: 11824275
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
ID: 11824318
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
ID: 11824533
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
ID: 11824563
Either.  Both will work as a DataSource.  
0
 
LVL 3

Author Comment

by:jayrod
ID: 11824659
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
ID: 11824677
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
ID: 11824697
yep... that makes sense let me try that real quick
0
 
LVL 3

Author Comment

by:jayrod
ID: 11825062
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
ID: 11825780
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
ID: 11830527
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
ID: 11851669
Had to go with another round about solution...
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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