[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

combo box in c# windows form

Posted on 2007-08-09
6
Medium Priority
?
2,655 Views
Last Modified: 2013-12-17
i had a combo box  the field length of it is 1 char in database but the user should be able to see "default " and "override" text in combobox
so how can i set the properties for that or how can i do it manualy please help me .
or else how i will store it in the collection so that it should be 1 char in storage but the displaymember should be overide or default
0
Comment
Question by:kranthi4uonly
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 19665581
you could use an array list to load all of the db values and then ad a 2nd dimension with the text that you would like to display.

ArrayList al = new ArrayList();

al.Add(new DisplayValues("text to display", <value from db>);

public class DisplayValues
    {
        private String displaytext;
        private Int32 dbvalue;
       
        public DisplayValue(String text, Int32 value)
        {
            displaytext = text;
            dbvalue = value;
        }
        public String DisplayText
        {
            get { return displaytext; }
            set {displaytext = value; }
        }
        public Int32 DBValue
        {
            get { return dbvalue; }
            set { dbvalue = value; }
        }
    }

then the combobox datasource can be set to this for display member and value member.

cmb.datasource = al;
cmb.displayMember = "text";
cmb.ValueMember = "value";


forgive me if this doesn't work out of the box but i am posting quickly. it should at least get you off on the right foot.
0
 

Author Comment

by:kranthi4uonly
ID: 19666490
hi
can't we set them in the properties of the "combo box"  i mean presing f4 not this properties

if not
coming to my question
the values doesn't exist in the database if i select "default" or "override" {and submits form) it should go as 1 char to the database but it should be dispalyed as "default" or "overide" in combobox
thanks regards
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 19667955
Typically, you do this by having a datasource that contains both the single character value and the display value.

Let's say you have a set of rows in this form, with a column for "DisplayValue" and a column for "DataValue", in a view called dvChoices.

Try:

choiceCombobox.DataSource = dvChoices
choiceCombobox.DisplayMember = "DisplayValue";
choiceCombobox.ValueMember = "DataValue";

David
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 22

Expert Comment

by:p_davis
ID: 19668935
that is what my post will do.-- it creates an array list with text that you will assign to whatever character you want.-- the arraylist becomes your datasource after you construct it. the valuemember is what gets saved to the database the display member is what is displayed to the user
0
 

Author Comment

by:kranthi4uonly
ID: 19688874
hi davis
where to set this one in ur solution is it in the properties or what

cmb.datasource = al;
cmb.displayMember = "text";
cmb.ValueMember = "value";
 
and one more thing is iam using  menu -->data--->datasources and if u do show datasources u wil get all the colum names of the datatable  so i selected combobox for that colum and draged it on form so automaticaly it will bind to the colum so how to do it fmr here please help me
0
 
LVL 22

Accepted Solution

by:
p_davis earned 1000 total points
ID: 19691046
well i would set that either in the constructor for the form after the initialization of the form objects or in a onformload event.

for this you aren't going to be able to keep the direct binding to the table -- you will have to set it to the arraylist.-- if you choose to use it. bind that column/table to a datatable and then you can populate your array list from there.
you can get access to each row doing something like

foreach(DataRow row in myDataTable.rows)
{
     if(row["columnname"].toString() == 0)
     {
          al.add(new DisplayValues("custom    text",ConvertTo.Int32(row["columnname"]))
     
     }
}
do that for each row in your datatable and then use the arraylist to bind.

place the creation of the arraylist before the datasource binding, again, either in the constructor or in an on form load event.

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 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