I have one RadComboBox.
I fill it with a function which returns for example {Stack, Over, Flow, StackOverFlow}
When I click RadCombobox that items are listed.
And I want to give first place for empty element.
And I tried to do below:
var stack= GetItems(SiteId);
RadComboBox1.Items.Add(new RadComboBoxItem("", "0"));
RadComboBox1.DataSource = stack;
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataBind();
RadComboBox1.AllowCustomText = false;
There is no change. Only {Stack, Over, Flow, StackOverFlow} are listed.
When I write below code
var stack= GetItems(SiteId);
RadComboBox1.DataSource = stack;
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
RadComboBox1.DataBind();
RadComboBox1.Items.Add(new RadComboBoxItem("xyz", "0"));
RadComboBox1.AllowCustomText = false;
Only {Stack, Over, Flow, StackOverFlow, xyz} are listed.
But I dont get the result which I want.
And the design side is below.
<telerik:RadComboBox ID="RadComboBox1" runat="server" Width="120px" MarkFirstMatch="true" Filter="Contains"></telerik:RadComboBox>
How can I do?
I want to list { " " , "Stack", "Over", "Flow", "StackOverFlow"}
Using your first choice, add RadComboBox1.AppendDataBoundItems = true before you call DataBind(). This means it won't clear out the existing items before adding the items from the data bind. You will need to manually clear the items beforehand if necessary:
RadComboBox1.Items.Clear();
RadComboBox1.ClearSelection();
When you set RadComboBox DataSource property it will clear all the items in the RadComboBox.Items then iterates through the IEnumerable object which is set to it and add them to the items collection. In your case you can add all of your items manually using radComboBox.Items.Add().
var stack= GetItems(SiteId);
//Add your empty item.
RadComboBox1.Items.Add(new RadComboBoxItem("", "0"));
//Add all the other items
foreach(var item in stack)
{
RadComboBox1.Items.Add(new RadComboBoxItem(item.Name, item.Id))
}
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
Or you can add your empty item to the collection first and then bind it to the RadComboBox(I assume stack is a List of StackItem)
List<StackItem> stack = GetItems(SiteId);
//Add your empty item.
stack.Insert(0, new StackItem(){Name = "", Id = 0});
//Set the DataSource
RadComboBox1.DataSource = stack;
RadComboBox1.DataTextField = "Name";
RadComboBox1.DataValueField = "Id";
Although usually it's not a good idea to change your collection(latter example) for such cases.
Related
I need some help.
I want to add an List as DataSource to a ComboBox.
The List contains 1 and more elements from type "Zaehler".
I use 3 comboBoxes in my GUI. Two times it works. I create a List Element and Add Elements of T. After this I set this List as DataSource. If I check with the debugger it says that the comboBox.Items = List..Count = comboBox.DataSource.Count.
private void LadeZaehlerDaten()
{
//NEUER ZÄHLER
List<Zaehler> zaehlerArten = new List<Zaehler>();
zaehlerArten.Add(new StromZaehler(1, "Stromzähler"));
zaehlerArten.Add(new GasZaehler(2, "Gaszähler"));
cb_ZaehlerArt.DataSource = zaehlerArten;
cb_ZaehlerArt.DisplayMember = "Name";
cb_ZaehlerArt.Sorted = true;
cb_ZaehlerArt.DropDownStyle = ComboBoxStyle.DropDownList;
Zaehler z = (Zaehler)cb_ZaehlerArt.SelectedItem;
// ... more code
}
This code works correctly... I have zaehlerArten with 2 Elements and in the ComboBox I have 2 Items bzw. DataSource.Count = 2.
after this I create a new Zaehler and add this to List zaehlerList that will contains all correct Zaehler.
This one goes wrong....
private void LadeVertragsDaten()
{
//NEUER VERTRAG
cb_VertragZaehler.DataSource = zaehlerList;
cb_VertragZaehler.DisplayMember = "Name"; //Property in Zaehler
cb_VertragZaehler.ValueMember = "ZaehlerNr"; //Property in Zaehler
//cb_VertragZaehler.Sorted = true;
cb_VertragZaehler.DropDownStyle = ComboBoxStyle.DropDownList;
if (cb_VertragZaehler.Items.Count < 1)
{
return;
}
else
{
Zaehler auswahl = (Zaehler)cb_VertragZaehler.SelectedItem;
}
}
If I check with the debugger ... it says das cb_VertragZaehler.Items is 0 but DataSource and zaehlerList count 2.
In my opinion I didn't do anything different. It should work the same like it does with LadeZaehlerDaten()
Since the Items of the ComboBox be 0 I can't use the information of this "Zaehler".
I did say DisplayMember is Name ... even through I did hope to use something like ZaehlerNr and Name ... booth are Properties of the class Zaehler.
Could someone say why it works in LadeZaehlerDaten() but not in LadeVertragsDaten()?
Picture: DebuggerInformation
And I really do not know why..
The list you try to assing should be immutable. Try to create a copy of it with cb_VertragZaehler.DataSource = new List<Zaehler>(zaehlerList);
DataSet dsCurrency = new DataSet();
dsCurrency = ParamCurrency.SelectCurrencys();
ddCurrencyField.DataSource = dsCurrency;
ddCurrencyField.DataTextField = "CurrencyName";
ddCurrencyField.DataValueField ="CurrencyCode";
ddCurrencyField.DataBind();
How to select a default value to the dropdownlist control using C#?
If you know the value will exist:
ddCurrencyField.FindItemByText("YourDefaultText").Selected = true;
else
ListItem selectedListItem = ddCurrencyField.Items.FindItemByText("YourDefaultText");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
};
You can also find item by value :
ListItem selectedListItem = ddCurrencyField.Items.FindByValue("YourDefaultValue");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
};
I assume in your datasource object (dsCurrency) is not parsing the default value for the dropdown.
So first you will have to add the default item. After binding the datasource do the following.
ddCurrencyField.Items.Insert(0, new ListItem("-- Select --",0));
With the above code you will have a default/first item selected as "--Select--". If it does not select the first item then simply set the SelectedIndex to 0.
There are 2 ways to set the default item after populating a dropdown.
you can use the "SelectedValue" property
you can use the "SelectedIndex" property
Most of the code samples are given in the previous answers. But I prefer to use the "FindByValue" method.
ddCurrencyField.SelectedIndex = ddCurrencyField.Items.IndexOf(ddCurrencyField.Items.FindByValue(myValue));
If you want to write a safe code please use the second option.
If this dropdown list is a combobox, use this:
ddCurrencyField.SelectedIndex = ddCurrencyField.Items.IndexOf("Wanted Value");
I'm trying to mark one of the combobox items as selected.
So I am building my combobox like this:
var drop = new Dictionary<int, string>();
while (RegReader.Read())
{
drop.Add(Convert.ToInt32(RegReader["intRulesID"]), RegReader["txtName"].ToString());
}
RegRuleDrop.DataSource = new BindingSource(drop, null);
RegRuleDrop.DisplayMember = "Value";
RegRuleDrop.ValueMember = "Key";
Now, one of the items within the RegRuleDrop should be pre selected based on a value from a reader above this code.
Now, the problem is that I need to select value based on the actual ListItem VALUE and not TEXT.
So as an example
drop.Add(1, "Test");
drop.Add(2, "Test2");
drop.Add(3, "Test3");
I need to find the index using 1,2 or 3 not Test, Test2 or Test3
Any ideas?
When you have the DataSource set to a BindingSource the only action needed to select an item given a value belonging to the ValueMember property is
drop.Add(1, "Test1");
drop.Add(2, "Test2");
drop.Add(99, "Test99");
drop.Add(3, "Test3");
.....
RegRuleDrop.SelectedValue = 99
I was create a Employee registration page with dropdown list.Its used for selecting priority for each user. suppose i want to edit some user detail in this time am using the below code to select the dropdown list. It returns repeat value with out first index. That means a user have 3rd priority means it select third priority with out having 1st priority.
foreach (DataRow dr in DS.Rows)
{
txtemail.Enabled = false;
pan_addEdit.Visible = true;
this.btnSave.Text = "Update";
lbluserid.Text = Convert.ToString(dr["fdluserId"]);
txtuername.Text = Convert.ToString(dr["flduser"]);
txtPass.Text = Convert.ToString(dr["fldpass"]);
txtemail.Text = Convert.ToString(dr["fldemail"]);
ddlstatus.SelectedValue = Convert.ToString(dr["fldstatus"]);
ddlusergroup.SelectedValue = Convert.ToString(dr["fldgroupId"]);
ddldept.SelectedValue = Convert.ToString(dr["flddept"]);
ddlperiority.SelectedValue = Convert.ToString(dr["fldperiority"]);
}
dropdown show the below value after binding
1st priority
2nd priority
3rd priority
dropdown show the below value after binding
3rd priority
2nd priority
3rd priority
please help me to fix this error..
You don't want to change the Text of the curently selected item. You want to change the selection itself. Therefor you could use the SelectedIndex or the SelectedValue property of the DropdownList.
ddlperiority.SelectedValue = Convert.ToString(dr["fldperiority"]);
Of course there must be a value that matches dr["fldperiority"]. Maybe this is the text field and you need the PK/FK field.
You can use the FindByValue method to search the DropDownList for an Item with a Value matching the parameter.
ddlperiority.ClearSelection();
ddlperiority.Items.FindByValue(dr["fldperiority"].ToString()).Selected = true;
Alternatively you can use the FindByText method to search the DropDownList for an Item with Text matching the parameter.
Before using the FindByValue method, don't forget to reset the DropDownList so that no items are selected by using the ClearSelection() method. It clears out the list selection and sets the Selected property of all items to false. Otherwise you will get the following exception.
"Cannot have multiple items selected in a DropDownList"
If still above answers are not working for you, try something like this
var item = Convert.ToString(dr["fldperiority"]);
switch(item)
{
case "1st priority":
ddlperiority.SelectedIndex = 0;
break;
case "2nd priority":
ddlperiority.SelectedIndex = 1;
break;
case "3rd priority":
ddlperiority.SelectedIndex = 2;
break;
}
Please do like below
ddlstatus.Items.Clear();
ddlperiority.Items.Clear();
ddlusergroup.Items.Clear();
ddldept.Items.Clear();
foreach (DataRow dr in DS.Rows)
{
txtemail.Enabled = false;
pan_addEdit.Visible = true;
this.btnSave.Text = "Update";
lbluserid.Text = Convert.ToString(dr["fdluserId"]);
txtuername.Text = Convert.ToString(dr["flduser"]);
txtPass.Text = Convert.ToString(dr["fldpass"]);
txtemail.Text = Convert.ToString(dr["fldemail"]);
ddlstatus.Items.Add ( Convert.ToString(dr["fldstatus"]));
ddlusergroup.Items.Add( Convert.ToString(dr["fldgroupId"]));
ddldept.Items.Add( Convert.ToString(dr["flddept"]));
ddlperiority.Items.Add( Convert.ToString(dr["fldperiority"]));
}
I have a sorted list. When I bind it to a listbox, it does not shows the item in an ordered manner.
territoryListBox.BeginUpdate();
this.Text = ((INamedEntity)_currentList[0]).Name;
territoryListBox.DataSource = _currentList;
territoryListBox.DisplayMember = "Name";
territoryListBox.Sorted = true;
territoryListBox.EndUpdate();
The first item in the list is, say, A. The this.Text shows "A", which is the first item in the list. But the listbox shows:
B
C
A
_currentList is a IList<>
Are you swallowing an exception? When I try this I get (when setting Sorted) an ArgumentException:
Items collection cannot be modified when the DataSource property is set.
IMO, sort the list first - and bind to that; however, a quick test shows that setting Sorted before setting the DataSource works too - i.e.
territoryListBox.Sorted = true;
territoryListBox.DataSource = yourListOfData;
territoryListBox.DisplayMember = "Name";
Just for fun, try ListBox.Sort. Obviously, that should not be necessary, but something's going haywire.