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
Solved

combo box question for window forms

Posted on 2004-08-17
14
194 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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 use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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