Solved

combo box question for window forms

Posted on 2004-08-17
14
191 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

11 Experts available now in Live!

Get 1:1 Help Now