• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2657
  • Last Modified:

combo box in c# windows form

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
kranthi4uonly
Asked:
kranthi4uonly
  • 3
  • 2
1 Solution
 
p_davisCommented:
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
 
kranthi4uonlyAuthor Commented:
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
 
anyoneisSoftware DeveloperCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
p_davisCommented:
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
 
kranthi4uonlyAuthor Commented:
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
 
p_davisCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now