C# ComboBox removing and adding - c#

Im trying to populate 5 different comboboxes with the same datasource. The datasource is from a class list with values like this (the values that arent displaymember im using to populate text-boxes adjacent to the combobox):
StandardList.Add(new Standard() { ID = 1, number = 91632, credits = 4,
uniEntrance = "Y", assessmentType = "Ext", standardType = "AS" });
This is how I have populated each combobox:
cboNumber1.BindingContext = new BindingContext();
cboNumber1.ValueMember = "ID";
cboNumber1.DisplayMember = "number";
cboNumber1.DataSource = StandardList;
cboNumber1.Refresh();
When I select an item from one particular combobox, I want to remove that same item from the remaining comboboxes, and then re add it to each combobox if that item is deselcted in the focussed combobox. I have not figured out how to 're add' yet, however the 'removing' part works for every item except when I try to select the LAST item in the combobox using
StandardList.RemoveAt(cboNumber1. SelectedIndex);
it comes up with "ArgumentOutOfRangeException". My question is how do I get around this? Also if I select a particular item in the focussed combobox, and it removes that same item from each remaining combobox, how would I re-add that particular item back, at the same position, to each combobox if I deselected that particular item in the focussed combobox?
This is my first post so sorry for any bad practice. Also please bear in mind im still a highschool student so my c# knowledge is not very broad and my coding practices may be inefficient/bad. I would appreciate any answers/solutions in simple terms even if it means inefficiencies. Thanks.

On Selection change of the 1st combo box try to set the filtered DataSource of other combo boxes by using "Where" of list class.
like:
StandardList.Where(item => item.ID != cboNumber1.SelectedItem.Value);
This will return a filtered list, assign this list to other combo boxes.
This will work for you. I guess..

Related

How to move Items from one list to another by selecting the items in a datagrid

I have a couple of datagrids that uses a list of items as a source. I would like to move the item from list1 to list2 when the item in the grid is selected when I click a button or when I double click any of the cells in the row where the item is shown. This process will delete the selected item in the first list, and add it to the second one (it will disappear from the grid and be added to another grid which is linked to the 2nd list).
The items in both lists are part of the same class and both use the same constructor, so all of their parameters are the same.
I have been looking around the internet and trying different things myself but i cant quite find the solution, up to now this is what I came up with, but i cant make it to work.
public void Gladiators_Data_Grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string ItemMoving =
Data_Grid.Rows[e.RowIndex].Cells[0].Value.ToString();
var item = List1.FirstOrDefault(x => x.Name == ItemMoving);
if (item != null)
{
List1.Remove(item);
List2.Add(item);
}
}
The solution to my problem doesn't have to follow the pattern that I tried to use, anything that works will be highly appreciated, thanks in advance.
1) if you are not allowing the user to reorder the items, use the index of the selected row. It will be much faster.
2) the rest of your process is correct. However, you forgot to rebind the datagrids to the new lists. The grids do not update their UI automatically.
Small pseudo example:
var item = List1[grid1.currentrow.index];
List1.Remove(item);
List2.Add(item);
Grid1.datasource=list1;
Grid2.datasource=list2;

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.

DropDownList Showing First Item Empty

Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.

combo box selected index changed all comboboxes are changed

I have two combo boxes in my form. i have binded these two with different database tables but with the same display member and some of data are repeated. When i change the index of first combo box ,the second combo box index also changed.
how to clear this problem. please any one give me the solution
Thanks
//dt1---> a b c d
//dt2---> x c a y
cmb_STable.SelectedIndex = -1;
cmb_STable.DisplayMember = "tablename";
cmb_STable.DataSource = dt1;
cmb_mTable.SelectedIndex = -1;
cmb_mTable.DisplayMember = "tablename";
cmb_mTable.DataSource = dt2;
// if cmb_mTable_SelectedIndexChanged with a
, the cmb_sTable also changed with a
Shot in the dark:
I assume you are using ADO.Net to databind the comboboxes. Depending on the API you will either have a bindingsource or a CurrencyManager in the background which has a copy of the databound list and a pointer to the current item.
My guess is that you have bound both lists to this datasource via the same CurrencyManager/BindingSource and moving it on one control moves it on the other.
You need to add a new BindingSource or currencymanager to stop this behaviour.
Your code still doesn't show everything.
It looks like there is a Databinding for the SelectedValue for both boxes. If they are bound to the same item (copy pasted?) , then that explains your problem.

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