I have populated the comboBox with the Data from SQLite
Here's how I did it.
conn.CreateTable<AccountName>();
var query = conn.Table<AccountName>();
myComboBox.ItemsSource = query.ToList();
So when I try to retrieve the selected ComboBox Item to a TextBlock, I get the output as
'Apps.Models.AccountName'
How can I retrieve the values?
Look for SelectedValue, SelectedItem Gets currently selected item in the ComboBox. Use either of these.
comboBox1.SelectedValue;
or
comboBox1.GetItemText(comboBox1.SelectedItem);
I finally found the answer by using this code.
Demo.Text = ((AccountName)myComboBox.SelectedItem).AccName;
Related
I am using a linq to sql for populating a combo box. to populate combobox i am using the following code on page load event:
ColdStoreDataContext csdc = new ColdStoreDataContext();
comboBox1.DisplayMember="Name";
comboBox1.ValueMember="AccountHeadId";
comboBox1.DataSourse=csdc.SupplierPurchase;
the above code is working properly but when I use the given code:
comboBox.SelectedValue="KAMAL SINGH S/O AJEET SINGH";
then it does not properly works means comboBox displayed null value.
Please help me to resolve this problem.
Your combo box has AccountHeadId as its ValueMember. When you set the combo box's SelectedValue, the box will look in its data source for an item whose AccountHeadId matches the value you just set to SelectedValue.
Try
comboBox1.SelectedValue=2;
I have a Winforms application and a combobox has it's datasource set to a List when the form loads. The data displays fine in the combobox.
Then after a user clicks a button I want to create a new List and assign that List as the datasource for the combobox.
The problem is that after setting the datasource to be the new List the items in the combobox don't change. Here is the code I'm using.
var newPersonList=new List<Person>(){//...};//Person has a property named 'Name'
dlCustomer.DataSource = newPersonsList
dlCustomer.DisplayMember = "Name"
dlCustomer.Refresh()
Does anyone know how to make the correct data be displayed in the combobox the second time I assign new data source for it?
You can clear the items and then set the DataSource as below
dlCustomer.Items.Clear();
dlCustomer.DataSource = newPersonsList;
dlCustomer.DisplayMember = "Name";
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.
I have added items in a combo box using datatable and rows. First it returned the value, and now it's returning dataRow. I don't know the reason why it's returning like that. Can anyone help?
string selectedValue = cbx_language.Text.ToString();
It should be
string selectedValue = cbx_language.SelectedValue
Use string selectedValue = cbx_language.SelectedItem.ToString
This way, it does not matter what type of items you add using Items.Add/Insert.
Is the value you are getting is System.data.datarow?
& if It is then Please set valueMember & DisplayMember properties of combo box to the column name u fetching in datatable.
try this
string selectedValue = cbx_language.SelectedValue.ToString()
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