ComboBox filling - c#

I have a problem filling my ComboxBox. Here´s my code:
string[] test = { "Ausgaben", "Eingaben" };
foreach (string r in test)
{
cbEinAus.Items.Add(r);
}
The values from the string array are in the ComboBox, but the selected item is a empty string.
I want one of this two string from the array to be my selected item.
I already tried with the SelectedItem property, but that doesn´t work.
Maybe its a simple solution... Can anybody help me please?

Use:
cbEinAus.SelectedIndex = 0;
You can replace the 0 with the zero based index of whichever item you want to select.

Try
cbEinAus.Items[vbEinAus.SelectedIndex]
instead of SelectedItem. usually works for me.

Try setting the SelectedItem as cbEinAus.Items[0]

You can set the Text property.
cbEinAus.Text = test[0];
More examples can be found at How do I set the selected item in a comboBox to match my string?

cbEinAus.SelectedIndex = 0;
replace item to index

Related

Delete ComboBox Item by Index

I'm seraching for a way to delete a specific item in a ComboBox by index.
I only found a way to delete the item by value with this code:
cbRooms.Items.Remove((ComboBoxItem)item))
Update
I want to use a code like that
ComboBox.Items.Remove(ComboBox.SelectedIndex);
You can remove item by index, using RemoveAt method:
comboBox.Items.RemoveAt(index);
It is as simple as :
cbRooms.Items.RemoveAt(0); //0 = Index
If you want the get the index of the selected value and remove it, you can do something like :
int SelectedIndex = cbRooms.SelectedIndex;
// OR LIKE : int SelectedIndex = cbRooms.FindString(textBox2.Text);
cbRooms.Items.RemoveAt(SelectedIndex); //0 = Index
Hope that what you are looking for and my answer helped you.

How do I get Index from listboxitem by its content?

Is there a way to get a Listbox Item Index by its content? Something like:
id = listbox.Items.Contains("text");
I know that this way I'll get a bool result, but I don't know how to get the item Index. If I could get the Index I could be able to remove items with
listbox.Items.RemoveAt(id);
In WPF
ListBox listBox = new ListBox();
int index = listBox.Items.IndexOf(item);
Look for the item's index, and then you can remove it with the given method:
int index = myListBox.Items.IndexOf(itemToSearch);
/*if there is no coincidence, it returns -1, so you must check for that return, else RemoveAt(-1) would give you a runtime error.*/
if (index >=0)
{
myListBox.Items.RemoveAt(index);
}
Assuming the item is not selected but for some reason you just want to find it in the list:
The listbox.items is a listbox object collection:
So:
id=listbox.items.IndexOf("text");
will do the trick.

How to get text from selected line in ListBox (c#)?

I want to get data from selected line in List Box. I use command:
string selected = ListBox1.SelectedItems[0].ToString();
But the result is:
ListVievItem: {here is correct value}
What should i do with this: "ListVievItem: {}"
string urItemText = ListBox1.SelectedItem.Text;
http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox_properties(v=vs.80).aspx
EDIT As suggested by John Willemse, ListBox can't have ListViewItems so it seems like this question relates to ListView rather than to a ListBox so the code in the answer is changed accordingly.
When you call it like this listView1.SelectedItems[0].ToString(); you are actually calling the ToString() method of the ListViewItem object which gives the unwanted result (first prints the class name and then the value). Each ListViewItem object has Text property from which you can get its text.
string selected = listView1.SelectedItems[0].Text;
Try something like this:
string selected = ListBox1.SelectedItems[0].Text;
Did you Try just: ListBox1.SelectedItem.Value

combobox item selected To be fixed

I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;

Delete selected items in a GridView

IList<object> itemsSelected = MyGrid.SelectedItems;
foreach (object itemSelected in itemsSelected)
{
MyGrid.SelectedItems.Remove(itemSelected);
}
I try remove selected items from a GridView but not all selected items are removed.
Could someone help me?
object[] itemsSelected = MyGrid.SelectedItems.ToArray<object>();
foreach (object item in itemsSelected)
{
MyGrid.Items.Remove(item);
}
I had the exact problem as well. I wrote something like this but it didn't delete all items too, just some of them :
foreach(var item in MyGridView.SelectedItems)
{
MyGridView.Items.Remove(item);
}
But write this which will delete all your selected items for sure :
while (YourGridView.SelectedItems.Count != 0)
{
YourGridView.Items.Remove(YouGridView.SelectedItem);
}
there isn't exception, when you use foreach with SelectedItems? When you remove item, SelectedItems array is modified and foreach throws an exception. (Though I've tried on ListBox control). Try to use for-operator and remove items from last to first by index.
Based on your answers in the comments, I assume you are working on a C# winforms application.
Is it possible that what you are actualy using is a ListBox and not a GridView?
If that is so, you should use the ClearSelected() method which unselects all items in the ListBox.

Categories

Resources