Set selected item in a databound list box - c#

I've got a wpf listbox that is bound to a datatable. At some times, I want to programmaticly change the selection of the listbox. I know the text of the item I want to select. But it doesn't work to set the listbox1.SelectedItem to the text I want because the type of the SelectedItem is System.Data.DataRowView.
If I have the text I want to select and the DataRow that I want to select, what is the easiest way select the associated row in the list box?

Search through your DataSet and find the appropriate DataRow. Then set SelectedItem to that DataRow.

If you know the text, then it would be:
ListBox1.SelectedValue = ListBox1.Items.FindByText("Two").Value;
You can also use the SelectedIndex property to set the selected value by 0-based index.

The ListBox control (both in Forms and WebControls) has a SelectedValue property that:
"Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value."
You could use this to select the item based on it's value, typically a unique key.
More info from MSDN:
System.Windows.Forms.ListControl.SelectedValue
System.Web.UI.WebControls.ListControl.SelectedValue

Related

How can i find item in wpf listbox having grouped list bind to it?

My listbox is having a grouped list so basically I want to find listbox group item index with item value. Listbox is having item source bind to it and is having DisplayMemberPath and SelectedValuePath set from code behind.
What I have tried yet are as follow:-
int index = istboxName.Items.IndexOf(ListBindToItemSource.particularParameterValue);
Give index=-1 always.
Another solution I tried is:
int index = ListboxName.Items.Groups.IndexOf(ListBindToItemSource.particularParameterValue);
Same result index=-1 always.
You should never need to access the items that way, instead access the item in your bound source and manipulate that. If you want to change anything in the view, like e.g. a Background, bind it on your item and change it at the source.

Eliminate null entry in combobox

Is there any property to remove the first (and empty) item in a combobox with style DropDownList ? In other words, I would like to choose the default selected item for a combobox.
I know i can validate the selected item with code, but i want to avoid showing message boxes to the user.
Set the comboBox.SelectedIndex property to 0 to set the selection to the first item in the combobox.
You should set the Text or SelectedIndex or SelectedValue property. In this way the combobox updates the text that's is showing and removes the first empty item (that actually is not a real item).

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.

C# Datagridview: get selected item in combobox columns

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

C#: How do you control the current item index that displays in a ComboBox?

I have set a list of items in a combobox, but when I debug the application, I have to select an item from the drop down to display an item by default. Is there a property? that you can set so that the combobox itemindex will always begin at 0 meaning the first item in the list at startup of the application?
this.comboBox1.SelectedIndex = 0;
You are gonna want to use the SelectedIndex attribute.
The SelectedItem property can only be read.
Using the SelectedText and SelectedValue Properties will change the Text and Value not which one is selected

Categories

Resources