Add "--Select--" in ComboBox - c#

I have a Combobox the data for which is being populating from Sharepoint List.
I need to add a value called "--Select--" in the combobox and set it as default upon launching the application.
I cannot add "--Select" in the Sharepoint list. Please assist on how can i do it.
Below is the code.
teamName = templatedata.getTeamName();
cmbteams.DataSource = teamName;

I assume Winform,
cmbteams.Items.Insert(0, "SELECT");
cmbteams.SelectedIndex = 0;

do like this:
cmbteams.Items.Insert(0, "Select");
or
cmbteams.Items.Add(new Item("Select", 0));
you can set default like this:
cmbteams.SelectedValue=0;
or
cmbteams.SelectedIndex=0;

Related

Winform Combobox with DataSource as List<int>

This may be a very simple question but I realized I could not get it work.
I have a Winform combo box with datasource as a List<int>
combo.DataSource = intList;
What do I put for .DisplayMember and .ValueMember in order to simply have a list of integer values? Not setting them will display nothing.
I have worked with other List<myObj> in which DisplayMember and ValueMember are myObj's properties. How about simple data type like int, string?
When retrieving the selected item, one could simply cast (int)(combo.SelectedItem) or have to go through the property corresponding to ValueMember?
The problem does not occur because you have a list of integers, it probably occurs because you add items to the list after assigning it to the .DataSource property. List does not have a mechanism to notify its container when items are added to or removed from it.
Either add items to the list before assigning it to the .DataSource property, or use a wrapper like BindingSource as Krishnraj Rana suggested.
Here BindingSource comes into picture. You can use it like this.
BindingSource bSource = new BindingSource();
bSource.DataSource = new List<int> { 1, 2, 3 };
combo.DataSource = bSource;
Though you can set datasource of combobox directly with list. like this -
combo.DataSource = intList;
This also works perfectly fine.
You can add items from list using foreach like this.
foreach (var v in intList)
{
comboBox1.Items.Add(v.ToString());
}

Need help in assigning ASP.NET form variables to embedded C# code

I am looking to embed C# code into an ASP.NET website.
How do I assign values selected from a drop-down menu on the ASP.NET site form into pre-determined code variables?
My drop down menu text will be text, but I would like to assign them in the code as an integer.
e.g if they selected the colour 'Blue' from the drop down menu, I would like it to assign '2' to my C# variable
Any help is much appreciated,
Thanks.
You would bind the drop down list like this, and assign the DataTextField, and the DataValueField
var source = new Dictionary<int, string>();
source.Add(0, "Text 1");
source.Add(1, "Text 2");
dropDown.DataSource = source;
dropDown.DataTextField = "Key";
dropDown.DataValueField = "Value";
dropDown.DataBind();
You can then access the value, and textx like this;
dropDown.SelectedItem.Value
dropDown.SelectedItem.Text
If dropdawn name is x then
Assign DropDawn Value to a variable Y
Y=deopdawn.selecteditem.value
int y=0;
y=dropdawl.SelectedIten.value;

Default load in combobox

If I want to load for example a default country when the combobox shows up ("Country: Lebanon"), how do I do so?
I am using VS2008 (C#). Thank you.
Add some items to the Combobox...it is just a sample, you can run a loop also to add items.
combobox1.Items.Add("USA");
combobox1.Items.Add("England");
combobox1.Items.Add("Kenya");
combobox1.Items.Add("South Africa");
Now your Combobox has four items. To select a specific country try this:
combobox1.Text = "USA";
To select a on the index basis, try this:
combobox1.SelectedIndex = 0; // For USA
Hope it helps.
You can use another method to add items in comboBox:
comboBox1.Items.Insert(0, "Egypt");
//the first item is the item index and the second is the item object.
You would usually just set myCombo.SelectedValue = "Whatever value" as soon as the form is loaded.
Use one of SelectedValue, SelectedIndex, SelectedItem or SelectedText properties of the ComboBox control.
You could try this:
myCombo.SelectedIndex = lebanonsIndex;
You can set it by using IndexOf in the Items collection
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Lebanon");// case sensitive
This could work. Copy this code in your app.config file within appsettings.
< add key="DefaultCountry" value="Lebanon" />
and copy this at ur win form where you want to view it.
combobox1.Text = System.Configuration.ConfigurationManager.AppSettings["DefaultCountry"].ToString();.<add key="DefaultCountry" value="Lebanon"/>
You can set selected index in Form load event
If Lebanon is first in comboboxItem selectedIndex = 0;
Example:
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}

How to set selected value from Combobox?

I use combobox in c# windows form. I bound the item list as below:
var employmentStatus = new BindingList<KeyValuePair<string, string>>();
employmentStatus.Add(new KeyValuePair<string, string>("0", "[Select Status]"));
employmentStatus.Add(new KeyValuePair<string, string>("1", "Contract"));
employmentStatus.Add(new KeyValuePair<string, string>("2", "Part Time"));
employmentStatus.Add(new KeyValuePair<string, string>("3", "Permanent"));
employmentStatus.Add(new KeyValuePair<string, string>("4", "Probation"));
employmentStatus.Add(new KeyValuePair<string, string>("5", "Other"));
cmbEmployeeStatus.DataSource = employmentStatus;
cmbEmployeeStatus.ValueMember = "Key";
cmbEmployeeStatus.DisplayMember = "Value";
cmbEmployeeStatus.SelectedIndex = 0;
I save the selected value in database eg.1 or 2. Now I want to set selected value from database item like:
cmbEmployeeStatus.SelectedValue =employee.employmentstatus;
But combobox not selected with value. How can I do that?
Try this one.
cmbEmployeeStatus.SelectedIndex = cmbEmployeeStatus.FindString(employee.employmentstatus);
In order to do the database style ComboBoxes manually trying to setup a relationship between a number (internal) and some text (visible), I've found you have to:
Store you items in a list (You are close - except the string,string - need int,string)
DataSource property to the list (You are good)
DataMember,DataValue (You are good)
Load default value (You are good)
Put in value from database (Your question)
Get value to put back in database (Your next question)
First things first. Change your KeyValuePair to so it looks like:
(0,"Select")
(1,"Option 1")
Now, when you run your sql "Select empstatus from employees where blah" and get back an integer, you need to set the combobox without wasting a bunch of time.
Simply: *** SelectedVALUE - not Item ****
cmbEmployeeStatus.SelectedValue = 3; //or
cmbEmployeeStatus.SelectedValue = intResultFromQuery;
This will work whether you have manually loaded the combobox with code values, as you did, or if you load the comboBox from a query.
If your foreign keys are integers, (which for what I do, they all are), life is easy. After the user makes the change to the comboBox, the value you will store in the database is SelectedValue. (Cast to int as needed.)
Here is my code to set the ComboBox to the value from the database:
if (t is DBInt) //Typical for ComboBox stuff
{
cb.SelectedValue = ((DBInt)t).value;
}
And to retrieve:
((DBInt)t).value = (int) cb.SelectedValue;
DBInt is a wrapper for an Integer, but this is part of my ORM that gives me manual control over databinding, and reduces code errors.
Why did I answer this so late? I was struggling with this also, as there seems to be no good info on the web about how to do this. I figured it out, and thought I'd be nice and post it for someone else to see.
In windows Appliation
we use like this
DDLChangeImpact.SelectedIndex = DDLChangeImpact.FindStringExact(ds.Tables[0].Rows[0]["tmchgimp"].ToString());
DDLRequestType.SelectedIndex = DDLRequestType.FindStringExact(ds.Tables[0].Rows[0]["rmtype"].ToString());
Use the SelectedIndex property for the respective employee status in the combo box.
Below will work in your case.
cmbEmployeeStatus.SelectedItem =employee.employmentstatus;
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.
EDIT
I think setting the Selected Item as below is incorrect in your case.
cmbEmployeeStatus.SelectedItem =**employee.employmentstatus**;
Like below
var toBeSet = new KeyValuePair<string, string>("1", "Contract");
cmbEmployeeStatus.SelectedItem = toBeSet;
You should assign the correct name value pair.
cmbEmployeeStatus.Text = "text"
I suspect something is not right when you are saving to the db. Do i understand your steps as:
populate and bind
user selects and item, hit and save.. then you save in the db
now if you select another item it won't select?
got more code, especially when saving? where in your code are initializing and populating the bindinglist
A possible solution:
cmbEmployeeStatus.SelectedValue = cmbEmployeeStatus.Items.FindByText("text").Value;
try this
combobox.SelectedIndex = BindingSource.Item(9) where "9 = colum name 9 from table"
To set value in the ComboBox
cmbEmployeeStatus.Text="Something";
Try this:
KeyValuePair<string, string> pair = (KeyValuePair<string,string>)this.ComboBox.SelectedItem;

How can I populate a drop down list control Collection?

How can I populate a drop down list control (ASP.NET) from a SPListTemplateCollection (SharePoint 2007) ?
SPWeb web = SPContext.Current.Web;
ddlTemplateList = new DropDownList();
ddlTemplateList.DataSource = web.ListTemplates;
ddlTemplateList.DataBind();
This code doesn't work in a right way... The name of the list template is not showed.
You need to specify DataTextField and DataValueField to make it work.
ddlTemplateList.DataSource = web.ListTemplates;
ddlTemplateList.DataTextField = "DisplayColumnName";
ddlTemplateList.DataValueField = "ValudColumnName";
ddlTemplateList.DataBind();
use DisplayMember and ValueMember property!
update
DisplayMember and ValueMember are property for WinForm controls.
For asp.net correct solution, as metioned by Muhammad, is to use DataTextField and DataValueField.
Try this one
List<SPWeb> lstSPWeb = web.ListTemplates
ddlTemplateList.DataSource = lstSPWeb;
ddlTemplateList.DataBind();
foreach (SPListTemplate lt in SPContext.Current.Web.ListTemplates)
ddlTemplateList.Items.Add(new ListItem(lt.Name, lt.Type.ToString()));

Categories