C# Combobox move item to bottom of the list - c#

I need to add "Select more..." to the bottom of the combobox items, like it done on SQL 2008 servers selector. Trying like this:
List<string> srvList = new List<string>();
srvList.Add("ff");
srvList.Add("jj");
srvList.Add("pp");
srvList.Add("<Select more...>");
ComboBoxServs.Items.AddRange(srvList.ToArray<String>());
But "Select more..." appears at the top of items.

As MSDN says:
If the Sorted property of the ComboBox is set to true, the items are
inserted into the list alphabetically. Otherwise, the items are
inserted in the order they occur within the array.
Try to set Sorted property to false:
ComboBoxServs.Sorted = false;
List<string> srvList = new List<string>();
srvList.Add("ff");
srvList.Add("jj");
srvList.Add("pp");
srvList.Add("<Select more...>");
ComboBoxServs.Items.AddRange(srvList.ToArray<String>());

You have to use the index of Insert method of Combobox control
myComboBox.Items.Insert(0, "Select more");
hope that help.
you may Refer Here also

Related

See no Items after adding them

My Listview show nothing after adding Items from a List into it. Why?
My List is not empty And my program goes into this loop.
So Is the code wrong for adding items into an listview? Because I saw many chatrooms and also from Microsoft that I can add them like this.
I tried it also with this code:listView1.Items.Add(new ListViewItem { ImageKey = "Person", Text = database1[counter1].name });
Here a picture of my imglist, and the list is chosen at thelistview:
enter image description here
You can't add items if there are no columns, or you can but they wont show up.
Have you added some columns to your listView?
//Adds needed columns
this.listView1.Columns.Add("<column_name>", 50);
If you have one column, you can simply add items by:
ListViewItem itm = new ListViewItem("my_item");
listView1.Items.Add(itm);
If you have multiple columns instead of a string, you can do the same but with a string array where the array size equals to the number of columns.
string[] items = new string[listView1.Columns.Count];
Try this in your code too, in my code only works with this:
this.listView1.View = View.Details;

Selecting Multiple ListBox items from GridView column

I have a databound Listbox with Multiselect enabled. On page load, I feed the information from a GridView column and select all the options that match, using this code:
string[] separators = { "<br />" };
String Departments = Session["ProjDept"].ToString();
string[] splitDepartments = Departments.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (var dept in splitDepartments)
{
listDepartment.SelectedIndex = listDepartment.Items.IndexOf(listDepartment.Items.FindByText(dept));
}
However, I am running into a strange issue: when there is only one department in the GridView column, the option in the listbox gets properly selected, but when there's multiple departments only the LAST department gets selected.
I've ran System.Diagnostics.Debug.Print(dept) within my foreach to ensure that all the values are getting passed and they all appear in the STDOUT, but the listbox still won't cooperate.
Any ideas as to how I can fix this -- or alternatively, what other code could I use to achieve the same results?
Thank you!
The SelectedIndex property only allows one value at a time, so you're resetting it with each iteration. That's why only the last one is being selected. You need to access the "Selected" property from the ListItem itself.
Without trying it myself, it should look something like:
foreach (var dept in splitDepartments)
{
int index = listDepartment.Items.IndexOf(listDepartment.Items.FindByText(dept));
listDepartment.Items[index].Selected = true;
}
As long as you do have SelectionMode="Multiple" - that code should work.

Top Item On Combo Box

I have windows forms project and List of type DateTime which I want to bind in combobox. I want on top of the combo box to have text All. How to do that ?
List<DateTime> dueDates = manager.GetUniqueDueDates();
cbDates.DataSource = dueDates;
For example
All
1/1/2001
1/1/2002
1/1/2003
You can use the Insert method of the items property to add the extra item.
cbDates.Items.Insert(0, "All");
This way your datasource does not need to be a list of string.
Update
As mentioned by #Hassan Nisar in the comments, it won't work if you bind the datasource, but you can add items using a loop(refer to #Hassan Nisar's answer for an example).
After binding with List<DateTime> you cannot insert item.
Argument exception will be raised Items collection cannot be modified when the DataSource property is set.
Skip binding with data source and add items by iterating through list:
List<DateTime> dueDates = manager.GetUniqueDueDates();
//cbDates.DataSource = dueDates;
foreach (var date in dueDates)
cbDates.Items.Add(date)
cbDates.Items.Insert(0, "All");
you need to get the list of dueDtes as string because "All" is string we cant add in Datetime list.
List<string> dueDates = manager.GetUniqueDueDates();
in GetUniqueDueDates function you need to add "All"
public List<string> GetUniqueDueDates()
{
List<string> uniqueDate=new List<string>();
uniqueDate.add("All");
//Rest of your code
}
Since your list is of type DateTime , you should first make it to List to make it work when you add "All"
List<string> dueDates = new List<string>();
dueDates.Add("All");
dueDates.Add(new DateTime(2001,03,01).ToString());
dueDates.Add(new DateTime(2002, 04, 01).ToString());
dueDates.Add(new DateTime(2003, 05, 01).ToString());
cbDates.DataSource = dueDates;
Try to sort all items after adding another 1 since its all dateTime after All .
Something like:
e.g: a was suppose to be your dataSource:
comboBox1.Items.AddRange(a.OrderBy(c => c).ToArray());
and property to true before anything else:
comboBox1.Sorted = true;

How to select an Item from asp:Dropdownlist on Page load from Code Behind C#?

I tried so many articles around, like below to get my task done, but didn't work as I always ends up with an NullReferenceException, I have bound a database table column to the Dropdown list, on page load i want to select an item based on the value from database which is one of those listed items. Please help me.
txt_examtype.DataSource = dt;//txt_examtype is the dropdownlist
txt_examtype.DataTextField = "ExamTypeName";
txt_examtype.DataValueField = "ExamTypeName";
txt_examtype.DataBind();
String examtype = dt.Rows[0]["ExamType"].ToString().Trim();
ListItem myitem = txt_examtype.Items.FindByValue(examtype);
txt_examtype.SelectedValue = myitem.Value;
try this code
txt_examtype.SelectedValue = dt.Rows[0]["ExamType"].ToString()
You should set SelectedIndex instead of SelectedValue. This is safe to use:
txt_examtype.SelectedIndex = txt_examtype.Items.IndexOf(txt_examtype.Items.FindByValue(examtype));

change selecteditem of dropdownlist after binding

i have a droplist in page that i bind this with code
Category catObj = new Category();
dropCat.DataSource = catObj.GetAllCategory();
dropCat.DataTextField = "Title";
dropCat.DataValueField = "CategoryID";
dropCat.DataBind();
i want change selected item of droplist with ths code
dropCat.SelectedIndex = Convert.ToInt32(catObj.ParentId);
but this code cant change the selected item
please help me
thank you all
I'll assume that you're trying to select by value and not by index since catObj.ParentId is likely to contain a CategoryId. In case my assuption is correct, you'll need to do this
dropCat.SelectedValue = catObj.ParentId.ToString();
DropDownList.SelectedIndex property refers to a position inside Items collection and not about the value of the item.

Categories

Resources