Adding a new value to the drop down list box - c#

I have a drop down list box which has one of the values to be others. I want to move these value to the last. Please help me with this. The code i am using is as follows
ddlAssetsCountryOthersone.DataSource = lstIMAssetsByCountryOthers;
ddlAssetsCountryOthersone.DataTextField = "AssetDescription";
ddlAssetsCountryOthersone.DataValueField = "AssetDescription";
ddlAssetsCountryOthersone.DataBind();
ddlAssetsCountryOthersone.Items.Remove(
ddlAssetsCountryOthersone.Items.FindByValue(
Constants.PhilosophyAndEmphasis.Other.ToString()));
How can i add the others back to the drop down list in the last

try this after your databind :
ddlAssetsCountryOthersone.Items.Add(new ListItem("Others", "Others"));
By the way if you use Insert method, you can insert the item you want to the position you want. For example the code below adds the Other option to the 4th order :
ddlAssetsCountryOthersone.Items.Insert(4, new ListItem("Others", "Others"));

you can try like
ListItem li = new ListItem("text", "value");
yourDropdown.Items.Insert(yourDropdown.Items.Count, li);
If you have 5 items in dropdown, it will return 5, since the insert index start from 0

If you the dropdownlist is databound you will not be able to add items to your control after the DataBind() call, and if you add them before they will be cleared anyway whe you call DataBind().
You can use Insert or Add after the data binding takes place, and you can specify the index of the item you're inserting. Insert is used to insert at the specific location, Add will append to the bottom, as in your case:
//after databind
//example with insert - (-1 as list is zero based)
ddlMyList.Items.Insert(noOfItems-1, "Other");
OR
//add
ddlMyList.Items.Add("Other");

Or:
ListItem li = ddlAssetsCountryOthersone.FindByValue(Constants.PhilosophyAndEmphasis.Other.ToString()));
ddlAssetsCountryOthersone.Remove(li);
ddlAssetsCountryOthersone.Add(li);
That should work, please test - and replace Add with Insert as per JohnIdol's suggestion...

When databinding, it is far easier to add/remove items from the list that is being databound than it is to add/remove items after the data binding takes place.
I would create a wrapper around lstIMAssetsByCountryOthers that makes the necessary changes into a new IEnumberable and returns that new object to be databound.

Related

How to populate one combobox with an SQL dataTable and with an item seperate from that table

Sorry if the title is a bit vague - it is difficult to capture what i want to do in one sentence, so I'll quickly explain what I want to achieve.
I want to bind an SQL table to a comboBox and have all its values (Item names for example) but I also wish to add one of my own items into the same comboBox, separate from the table (For example - "Add new" should then be inserted at the bottom of the list after the SQL table values have been loaded in the comboBox).
Is this possible? If so, how do I do it?
Thanks guys!
You can assign datasource in code-behind like this.
cmb1.DataSource=dt;
cmb1.DataBind();
cmb1.Items.Insert(dt.Rows.Count,new ComboBoxItem("Add New","0"));
cmb1.appenddatabounditems="true";
You can use Insert, you have to specify index and new item to add in list.
cmb1.Items.Insert(tabl.rows.count,"Add new");
OR
ComboboxItem item = new ComboboxItem();
item.Text = "Add new";
item.Value = 0;
cmb1.Items.Add(item);
If you not specify any index it will append at last

Add a Default Item at last position of DataList - ASP.NET

You can create a DataList easily in asp.net, I need to add a default item at last position of Datalist, for example an item named "Add new Item", if Datalist is empty I can only see this default item if not I see it in last position. Hope I'm clear enough. Is this possible? if yes, HOW?
Assuming that you're data binding your DataList, you just need to add "Add New Item" to your data source before calling DataBind on your DataList. I don't know what your data source is but the logic should be similar to whatever you're using. If you're binding to a DataTable you will need to make sure that the sort makes your "Add New Item" entry be the last one. You may need to add a sort column or something that you give your own incrementing id to and sort based on that column.
List<string> values = new List<string>();
values.Add("Item 1");
values.Add("Item 2");
values.Add("Add New Item");
DataList dataList = new DataList();
dataList.DataSource = values;
dataList.DataBind();

Keep ListBox sorted, but with a particular entry always pinned to the top

I have a ListBox filled with Countries (which I take from the Active Directory). I want the list to be sorted, but also I want one entry "All" to be at the very top.
How can I do this?
If you are binding the data in code behind you can insert a Listitem at index 0.
ListItem myItem=new ListItem("ALL","value");
myListbox.Items.Insert(0, myItem);
I would sort the list items first before binding to you ListBox. There are several options for doing this depending on what your data source is i.e. DataTable, List, Dictionary etc. To insert an item use code below.
lstCountries.Items.Insert(0, new ListItem("All", "0"));
After you load the data (i.e Countries), add the ListItem as follows:
myListbox.Items.Add(new ListItem() { Text = "All", Value = "" });
myListbox.SelectedIndex = 0; //This line will selected the first item on your ListBox.
Here, you might consider which action take if the ListBox with text "All" is selected.

Sort Checkbox List on the basis of checked property

I have a check-box-List which I bind to a master dataTable(DTA)... I have another dataTable (DTB) which has the values that needs to checked in the check-box-List... So I loop through all items in the check-box-list to see if it exists in the DTB and set checked = true for those items that exists.
Now I want to show the checked items first in the Check-box-list box and the unchecked items below that.
Is there any way I can do it... A similar solution for List-Box could also be helpful. A Javascript hint is welcome too.
Thanks
- Raja
if you want to sort the check-box-list on the server side, you can first add the items from DTB to the check-box-list and set their Selected value to true, then add the others form DTA and for each item in DTA make sure it's not already in the list of items.
when inserting the two lists make sure they are sorted according to a secondary sort criteria if you need one.
if you don't need the sorting to take place on the server side you can use jquery to do that quite easily.
you need to get the check_box_list_client_id from the server, you can do that using
$('#<%= CheckBoxList1.ClientID %>') jquery selector.
$(function() {
// get the containing element - should be an HTML table
var cbl = $('#check_box_list_client_id');
// check if the jquery element has any items in it
if (cbl.length) {
// get all the table rows, and filter out all those which
// doesn't contain a checked checkbox
var cbElements = cbl.find('TR').filter(function(index, element) {
return $(this).find('input:checked').length;
});
// take each table row containing a checked checkbox and place it
// at the top of your check-box-list element we called cbl
cbElements.each(function() {
$(this).prependTo(cbl);
});
}
});
thats it, hope it got you where you needed.

List View in C#

I have a ListView in my C# and I want to select an item based on a number which I give. Is this possible ?
Example: if my List which has add , multiply and divide as elements in list . so if I give 2 it must select multiply. All these must be done programatically
Looks like you want to select an item by it's index.
If it's for WinForms, you can clear the SelectedIndices collection, and add your item index:
listView.SelectedIndices.Clear();
listView.SelectedIndices.Add(yourIndex);
For WebForms, you have the SelectedIndex property:
listView.SelectedIndex = yourIndex;
Remember that the indexes are zero-based.
If it's the ListItem value is 2 - I'd simply go with:
_listView.SelectedValue = "2";
or perhaps more correctly:
_listView.SelectedValue = _input.ToString();

Categories

Resources