Solved

combo box question for window forms

Posted on 2004-08-17
14
193 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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