DataBind listBox selected item to textboxes - c#

I have ListBox data bind to list of users (collection):
usersListBox.DataSource = null;
usersListBox.DataSource = _users;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
Now I want properties of selected item to be shown in different text boxes, so I do the binding:
nameTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Name");
loginTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Login");
When form load I can see that values of selected item appear in textboxes, but when selected item in listBox is changed, values in text boxes are still the same. Do I have to catch selectedItemChanged of listbox and repeat binding of textboxes? Or I'm missing something and values in textboxes should change with changing selected item?

If someone needs answer: you have to create binding source object and assign it list box and textboxes:
usersBindingSource = new BindingSource();
usersBindingSource.DataSource = _presenter.Users;
usersListBox.DataSource = usersBindingSource;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
nameTextBox.DataBindings.Add("Text", usersBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
loginTextBox.DataBindings.Add("Text", usersBindingSource, "Login", true, DataSourceUpdateMode.OnPropertyChanged);

Related

Trying to disable few items in a ListBox c#

I'm trying to disable a few items dynamically from a Listbox. I used the following code:
How To Disable Selected Item In List Box
But, all I see is System.Data.DataRow in the Listbox, not the actual string.
I tried adding new BindingContext()
lbInsTypes.BindingContext = new BindingContext();
Also,
lbInsTypes.DataBindings.Add("DisplayMember", dtInsType, "InsuranceDescription", false, DataSourceUpdateMode.OnPropertyChanged);
but nothing worked.
DataSet dsInsType = db.GetAllInsuranceTypes(InstutionId);
lbInsTypes.DisplayMember = "InsuranceDescription";
lbInsTypes.ValueMember = "InsuranceTypeId";
lbInsTypes.DataSource = dsInsType.Tables[0];
I want to see the actual InsuranceDescription values in the ListBox from the table. Please show me how to use datasource with custom ListBox control.

How to suggest ComboBox values by searching

I'm creating a windows application and I want to show combo box value by filtering from the user input value. There is a property called AutoComplete but I'm assigning values for comboBox items using an object. So I couldn't fill AutoCompletedSource. How can I assign my item list to it?
There are two main ways to get autocomplete on a combo box. The first is to set the source to the comboBox.Items:
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.Items.AddRange(new []{"Omg", "So Kewel"," I love it"});
The second is to set up a custom source. In the second case the drop down arrow will reveal nothing but the autocomplete will show when you start typing.
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
var list = new List<string>() {"Omg", "So Kewl", "I love it"};
var collection = new AutoCompleteStringCollection();
collection.AddRange(list.ToArray());
comboBox1.AutoCompleteCustomSource = collection;

how to select specific drop down list Item

ddlCountry is already bind
ddlCountry.DataValueField= "CountryId"
ddlCountry.DataTextFiled= "CountryName";
ddlCountry.DataSource= objCountry.Select();
ddlCountry.DataBind();
using above technique.
Now on gridview selectedIndex changed event i want to set the Value of dropdown. how can i do this??
behind gridview selected index change i am doing this code
ddlCountry.SelectedItem.Text = gvCountry.SelectRow.Cells[1].text;
but this is making duplicate of the item
so what i need to do which ddl property will select my desired text ???
You can do as follows
string gridText = gvCountry.SelectRow.Cells[1].text;
if (ddlCountry.Items.FindByText(gridText) != null)
{
ddlCountry.ClearSelection();
ddlCountry.Items.FindByText(gridText).Selected = true;
}
It should work
ddlCountry.Items.FindByText(gvCountry.SelectRow.Cells[1].text).selected = true;

ComboBox.SelectedValue not working

What is wrong with this code?
myComboBox.Items.Clear();
myComboBox.Items.AddRange(new string[]{"one","two"});
myComboBox.SelectedValue = "one";
It is showing up with nothing selected.
If you populate the combobox like this:
myComboBox.Items.AddRange(new string[]{"one","two"});
You must use the ComboBox.SelectedItem or the ComboBox.SelectedIndex property to set/get the selected item:
myComboBox.SelectedItem = "one"; //or
myComboBox.SelectedIndex = 0;
The ComboBox.SelectedValue property is inherited from
ListControl and must be used ONLY when:
the control is bound to a DataSource
and ValueMember and DisplayMember properties are definied.
A couple of different options:
1) change SelectedValue to SelectedIndex
myComboBox.SelectedIndex = 0; //your first item
Please ignore this, this is for asp.net
2) add in ListItems manualy
myComboBox.Items.Clear();
myComboBox.Items.Add(new ListItem() { Text = "one", Selected = true };
myComboBox.Items.Add(new ListItem() { Text = "two" };
Just make sure you don't have more than one item selected at a given time.

Bindingsource datagridview click fill data in textbox

I have a datagridview which is bind from a bindingsource I really try to find out how onclick row on datagridview the row data fill in the TextBox. Because I want to show some data on combobox according selection on datagridview. My code for bind the datagridview is below
private void FillGrid()
{
if (_fisComp == null)
_fisComp = new FiscalComponent();
List<FiscalPeriod> _fisPeriod = _fisComp.GetAllByFiscalPeriod(Convert.ToString(fiscalYearComboBox.SelectedValue));
fiscalPeriodBindingSource.DataSource = _fisPeriod;
fiscalPeriodDataGridView.Refresh();
}
I am binding my datagridview by list<> type . The problem is that I am getting data from my stored procedure Y~N` and i want to display in combobox in Yes or No
This code is where I am binding my combobox
public void FillDropdown()
{
var itemsleaverun = new BindingList<KeyValuePair<string, string>>();
itemsleaverun.Add(new KeyValuePair<string, string>("Y", "Yes"));
itemsleaverun.Add(new KeyValuePair<string, string>("N", "No"));
leaveRunTypeComboBox.DataSource = itemsleaverun;
leaveRunTypeComboBox.ValueMember = "Key";
leaveRunTypeComboBox.DisplayMember = "Value";
leaveRunTypeComboBox.SelectedIndex = 0;
}
To bind one of the properties of FiscalPeriod to your combobox. I suppose you want to bind the Property X which can have values "Yes" or "No". You can bind it to your combobox like this:
yourComboBox.DataBindings.Add("Text", fiscalPeriodBindingSource, "X");
The Property X should be able to have values which are contained in the Items list of your comboBox. Because your combobox has a DataSource, they should be contained in the values list for the DataSource DisplayMember.

Categories

Resources