List View in C# - 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();

Related

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.

Getting the SelectedIndex of a LongListSelector Item

I have a WP8 DataBound app with an ItemViewModel bound to a LongListSelector.
Quite simply, when the user taps on an item in the list, I need to retrieve the index number of the item selected for future use. (0 is first in the list, 1 is second, etc.)
So, just as this might retrieve a certain property of the selected item:
string whateverProperty = (MyLongListSelector.SelectedItem as ItemViewModel).WhateverProperty;
I need something like this (obviously made up code):
int indexNumber = (MyLongListSelector.SelectedItem as ItemViewModel).GetSelectedIndex();
I think the SelectedIndex property is the thing I need but I can't figure out how retrieve it.
Thank you!
EDIT: SOLVED! The following gets me exactly what I was looking for:
int selectedIndex = App.ViewModel.Items.IndexOf(MainLongListSelector.SelectedItem as ItemViewModel);
I had the same problem. You need to use the ItemSource to retrieve the index. It should match your data template index for index.
int selectedIndex = selector.ItemsSource.IndexOf(selector.SelectedItem as ItemViewModel);
selector references the LongListSelector object sender. Hope this helps!

Sorting a ListBox ListItems with LINQ?

I'm trying to sort the items in a ListBox. However, upon doing so the Item Value gets set to the Item Text. Any help would be appreciated.
lbxCustomers.DataSource = lbxCustomers.Items.Cast<ListItem>().Reverse().ToList();
lbxCustomers.DataBind();
May be first you should store the list in generic collection and then sort it. Something like this:
List<ListItem> list = new List<ListItem>(lbxCustomers.Items.Cast<ListItem>());
list = list.OrderBy(li => li.Text).ToList<ListItem>();
lbxCustomers.Items.Clear();
lbxCustomers.Items.AddRange(list.ToArray<ListItem>());
Try resetting the DisplayMember and ValueMember properties of the Listbox after setting the DataSource. A lot of times if a control is already bound and you set the DataSource again, then it drops the DisplayMember/ValueMember properties.
Try sorting the list separately.
Point the Text value and Data value in the next step.
Databind the control.

Adding a new value to the drop down list box

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.

Dropdowns filled with same list item

I have a Gridview in which i have two templatefields of dropdownlist. I bound them on runtime with same list item.
li = new listitem ("1","1");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("2","2");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("3","3");
dl1.items.add(li);
dl2.items.add(li);
dl1.selectedvalue = "2";
dl2.selectedvalue = "3";
After executing above, dl1 & dl2 both show me "3" as selected value. Why?
I know the work around of using 2 different listitems while binding but i wanna know why the above happens?
Looking at just the last part of the code: you've got a single list item, and it's appearing in two different lists. But it's still one object. How would you expect one object to have two different values for a single property (SelectedValue)?
The ListItem class has a property "Selected" which marks if the item is selected. I haven't checked the DDL SelectedValue property to see what it does, but my guess is that the ListItem.Selected property is being set to true, and since you are using the same object in both drop-down lists, it is being marked as 'selected' in both.
I'm sure if this was a multi-select list, both "2" and "3" would be marked as 'selected'.
You have to instantiate each list item for each drop down list.
ListItem li1 = new ListItem("1","1");
dl1.items.add(li1);
ListItem li2 = new ListItem("1", "1");
dl2.items.add(li2);
Edit:
Jon described what I want to mean. You have only one object that has a value. So don't expect different values for each drop down list.
When you set dl1 to "3" then both of them will get the same value because both drop down lists reference to same object!
the listitem is being shared among the two drop downs. when you set the selected value for one of the drop downs it sets the list item as being selected. since the listitem is being shared it's selected in both drop downs
I would think it would be a ref vs value problem. I am sure both d1 and d2 would be pointing to the same spot in memory if the are added from the same list item...

Categories

Resources