I want to set the selecteditem attribute for an ASP.Net dropdownlist control programmatically.
So I want to pass a value to the dropdownlist control to set the selected item where the item is equal to the passed value.
Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.
list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();
list.SelectedValue = myValue.ToString();
The value of the myValue variable would need to exist in the property specified within the DataValueField in your controls databinding.
UPDATE:
If the value of myValue doesn't exist as a value with the dropdown list options it will default to select the first option in the dropdown list.
ddlData.SelectedIndex will contain the int value To select the specific value into DropDown :
ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
return type of ddlData.Items.IndexOf(ddlData.Items.FindByText("value")); is int.
Here is the code I was looking for :
DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByText("PassedValue"));
Or
DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByValue("PassedValue"));
Well if I understood correctly your question. The Solution for setting the value for a given dropdownlist will be:
dropdownlist1.Text="Your Value";
This will work only if the value is existing in the data-source of the dropdownlist.
If you need to select your list item based on an expression:
foreach (ListItem listItem in list.Items)
{
listItem.Selected = listItem.Value.Contains("some value");
}
Just Use this oneliner:
divisions.Items.FindByText("Some Text").Selected = true;
divisions.Items.FindByValue("some value").Selected = true;
where divisions is a dropdownlist control.
Hope it helps someone.
var index = ctx.Items.FirstOrDefault(item => Equals(item.Value, Settings.Default.Format_Encoding));
ctx.SelectedIndex = ctx.Items.IndexOf(index);
OR
foreach (var listItem in ctx.Items)
listItem.Selected = Equals(listItem.Value as Encoding, Settings.Default.Format_Encoding);
Should work.. especially when using extended RAD controls in which FindByText/Value doesn't even exist!
ddList.Items.FindByText("oldValue").Selected = false;
ddList.Items.FindByText("newValue").Selected = true;
On load of My Windows Form the comboBox will display the ClassName column of my DataTable as it's the DisplayMember also has its ValueMember (not visible to user) with it.
private void Form1_Load(object sender, EventArgs e)
{
this.comboBoxSubjectCName.DataSource = this.Student.TableClass;
this.comboBoxSubjectCName.DisplayMember = TableColumn.ClassName;//Column name that will be the DisplayMember
this.comboBoxSubjectCName.ValueMember = TableColumn.ClassID;//Column name that will be the ValueMember
}
Safety check to only select if an item is matched.
//try to find item in list.
ListItem oItem = DDL.Items.FindByValue("PassedValue"));
//if exists, select it.
if (oItem != null) oItem.Selected = true;
ddlemployee.DataSource = ds.Tables[0];
ddlemployee.DataTextField = "Employee Name";
ddlemployee.DataValueField = "RecId";
ddlemployee.DataBind();
ddlemployee.Items.Insert(0, "All");
Related
When I was working on one of my projects I was trying to write to a datasourced combobox and then write a value into the combobox like below:
//Create list for combobox
List<string> companyList= new List<string>() { "", "Company1", "Company2" };
//Datsource list to combobox
cbCompanyName.DataSource = companyList;
//If form is set to import data and the billing address is not null
if (importAddress && StaticValues.billAddress != null)
{
//Fill all fields with data from Static Values class
cbCompanyName.Text = StaticValues.billAddress.CompanyName;
cbCountry.Text = StaticValues.billAddress.Country;
}
else
{
//Set country to US
cbCountry.SelectedIndex = 0;
}
however the line cbCompanyName.Text = StaticValues.billAddress.CompanyName; ran without writing any text to the combobox, until I set the selected index of the combobox to -1. What does setting the combobox selected index to -1 do that would change this as apposed to setting the selected index to 0?
Setting the SelectedIndex on a ComboBox to -1 deselects (SelectedItem is NULL). Setting to 0 selects the first item in Items
Combobox needs to know what is my value and display member,
giving the datasource is not enough for it.
I think you can use like this or
// comboBox.DisplayMember = "Text";
// comboBox.ValueMember = "Value";
int =0;
companyList.forEach(x=> comboBox.Items.Add(new { Text = x.toString(), Value =i++ }));
comboBox1.SelectedIndex = 0;
you can look this article
similar question and answers
ddlCountry is already bind
ddlCountry.DataValueField= "CountryId"
ddlCountry.DataTextFiled= "CountryName";
ddlCountry.DataSource= objCountry.Select();
ddlCountry.DataBind();
using above technique.
Now on gridview selectedIndex changed event i want to set the Value of dropdown. how can i do this??
behind gridview selected index change i am doing this code
ddlCountry.SelectedItem.Text = gvCountry.SelectRow.Cells[1].text;
but this is making duplicate of the item
so what i need to do which ddl property will select my desired text ???
You can do as follows
string gridText = gvCountry.SelectRow.Cells[1].text;
if (ddlCountry.Items.FindByText(gridText) != null)
{
ddlCountry.ClearSelection();
ddlCountry.Items.FindByText(gridText).Selected = true;
}
It should work
ddlCountry.Items.FindByText(gvCountry.SelectRow.Cells[1].text).selected = true;
What is wrong with this code?
myComboBox.Items.Clear();
myComboBox.Items.AddRange(new string[]{"one","two"});
myComboBox.SelectedValue = "one";
It is showing up with nothing selected.
If you populate the combobox like this:
myComboBox.Items.AddRange(new string[]{"one","two"});
You must use the ComboBox.SelectedItem or the ComboBox.SelectedIndex property to set/get the selected item:
myComboBox.SelectedItem = "one"; //or
myComboBox.SelectedIndex = 0;
The ComboBox.SelectedValue property is inherited from
ListControl and must be used ONLY when:
the control is bound to a DataSource
and ValueMember and DisplayMember properties are definied.
A couple of different options:
1) change SelectedValue to SelectedIndex
myComboBox.SelectedIndex = 0; //your first item
Please ignore this, this is for asp.net
2) add in ListItems manualy
myComboBox.Items.Clear();
myComboBox.Items.Add(new ListItem() { Text = "one", Selected = true };
myComboBox.Items.Add(new ListItem() { Text = "two" };
Just make sure you don't have more than one item selected at a given time.
I have a drop down list control on my web page. I have bind the datatable to the dropdownlist control as follows -
lstDepartment.DataTextField = "DepartmentName";
lstDepartment.DataValueField = "DepartmentID";
lstDepartment.DataSource = dtDept;
lstDepartment.DataBind();
in the page load event i want to set the default value to the drop down list control from my other table field.
how to do this?
After your DataBind():
lstDepartment.SelectedIndex = 0; //first item
or
lstDepartment.SelectedValue = "Yourvalue"
or
//add error checking, just an example, FindByValue may return null
lstDepartment.Items.FindByValue("Yourvalue").Selected = true;
or
//add error checking, just an example, FindByText may return null
lstDepartment.Items.FindByText("Yourvalue").Selected = true;
if you know the index of the item of default value,just
lstDepartment.SelectedIndex = 1;//the second item
or if you know the value you want to set, just
lstDepartment.SelectedValue = "the value you want to set";
Assuming that the DropDownList control in the other table also contains DepartmentName and DepartmentID:
lstDepartment.ClearSelection();
foreach (var item in lstDepartment.Items)
{
if (item.Value == otherDropDownList.SelectedValue)
{
item.Selected = true;
}
}
lstDepartment.DataTextField = "DepartmentName";
lstDepartment.DataValueField = "DepartmentID";
lstDepartment.DataSource = dtDept;
lstDepartment.DataBind();
'Set the initial value:
lstDepartment.SelectedValue = depID;
lstDepartment.Attributes.Remove("InitialValue");
lstDepartment.Attributes.Add("InitialValue", depID);
And in your cancel method:
lstDepartment.SelectedValue = lstDepartment.Attributes("InitialValue");
And in your update method:
lstDepartment.Attributes("InitialValue") = lstDepartment.SelectedValue;
I can't get value from ComboBox in WinForms using C#.
I have a ComboBox populated with a list of values and I have set ValueMember and DisplayMember.
Now, I have to find the value of the selected ComboBox item and select the matched item in UI.
Here is what I mean:-
I loaded the ComboBox like this :-
var list = (from l in db.Loc
orderby l.LName ascending
select l).ToList();
list.Insert(0, new Loc { ID = "-1", Name = "--Select--" });
cmb1.BindingContext = new BindingContext();
cmb1.DataSource = list;
cmb1.DisplayMember = "Name";
cmb1.ValueMember = "ID";
Now on an event, I am trying to match value (ID) and select the item. It's easy if I match Text property:
cmb1.Text = data.Name;
But How to match the value?
Something like this:-
cmb1.Value = data.ID;
If you only know the ID of the item you can also use:
cmb1.SelectedValue = data.ID;
This should work:
cmb1.SelectedValue = data.ID;
Why would you like to assign you "matched" value to the ComboBox Value property?
As soon as you have correctly set DisplayMember and ValueMember and you DataSource implements both as properties the values will be autoamatically "matched", e.g. you can read the Value property in you event handler to get this "matched" value.
data must be in the list binded to the combobox, then:
cmb1.SelectedItem = data
or, if it's not (you retrieved another instance from somewhere):
cmb1.SelectedValue = data.ID
First of all: cmb1.Text = text; changes the text of the ComboBox to the specified value. It doesn't select the item with the text that matchs the specified value.
Use cmb1.SelectedValue = value; to select the item with the speciefied value.
You can get the index using Combo1.SelectedIndex property. You can get the item using either Combo1.SelectedItem or Combo1.Items[Combo1.SelectedIndex]