I'm trying to populate a combo box from a dataset, then bind it to a different data row.
The datarow contains a single customer records.
The dataset contains 1 table with title_id (an int) and title (the text description).
I set the value and display members, and the datasource of the combo box:
cbxTitle.ValueMember = "title_id";
cbxTitle.DisplayMember = "title";
cbxTitle.DataSource = dsTitles.Tables[0];
Next I set the data binding to the data row with the customers details:
cbxTitle.DataBindings.Add("Text", drCustomer.Table, "title_id");
When I run it, the combo box displays the number 2 (the value of title_id in the data row).
If I click it, it contains Mr, Mrs, Miss etc as i'd expect (Mr = 2)
What I can't figure out is how to get it to display 'Mr' instead of 2?!
How about:
cbxTitle.DataBindings.Add("SelectedValue", drCustomer.Table, "title_id");
Please refer to these links too:
Entity Framework 4 Databinding on WinForms ComboBoxes with Foreign Keys
Why does the ComboBox.SelectedValue DataBinding context gets cleared when BindingList changes?
Related
I have developed a small application but ran into this problem, so I'm starting with a clean slate to try and isolate it. So far, I made a very simple Windows form (VS 2017) application connecting to an SQL data table. On the form, I have a combo box for selecting a table row that is bound to the data set. I have it displaying an order number, and have its value member as an EntryID number as shown, which is also the "selected value".
I also have a simple text box on the form displaying the EntryID.
When I run the app, the combo box initially displays an ordered list of order numbers like this:
Before selecting an item, if I scroll through the list using the form's tool bar selector, the EntryID text box value corresponds to the combo box value, so Ord40 selects the last data set row (where EntryID = 1003).
When an item is selected, however, the combo box list order changes. For example, after a few selections, I get:
But if I select the last display item, "Ord 20", I still get the data row EntryID = 1003. In other words, although the bound data set does not change, the combobox scrambles the displayed item. Put yet another way, if the combobox has a set of display fields and a corresponding set of value fields, the display field text get out of sync with the underlying value.
I hope that makes sense. This is straight, out of the box, no altered code on a fresh project, and I have tried different settings on the "selected value" property.
Any help would be much appreciated.
James
You are binding SelectedValue of the ComboBox to the same data source which you use as data source of the control. Setting DataSource is just for showing items in dropdown. Setting SelecetdValue if for changing the value of bound property/column.
You never want to bind SelectedValue to the same data source which you use for DataSource.
Just as a an example, assuming you have the following tables:
Product (Id, Name, CategoryId, Price)
Category (Id, Name)
Then if you want to show a ComboBox for CategoryId in the edit form of Product, the setup of the categoryIdComboBox should be:
DataSource: categoriesDataSource
DisplayMember: Name
ValueMember: Id
SelectedValue: productsBindigSource - CategoryId
I have a gridview as you can see here:
As you can see i read the data in materialrequestcontractorId i have a listbox that this listbox reads its value from the database as you can see here:
List<MaterialRequestContractor> lstMRC = _materialRequestContractorRepository.Get().ToList();
foreach (MaterialRequestContractor VARIABLE in lstMRC)
{
LstMaterialRequestContractorId.Items.Add(VARIABLE.Id);
}
But the problem is i need to show my user the name of my MaterialRequestContractor not its id ,the id should be the value of my name and be saved in database .but in devexpress i can't assign this value and text to list box?
I have a record in my MaterialRequestContractor database with id=1
DevExpress provides several lookup editors for WinForms (LookUpEdit, GridLookUpEdit, SearchLookUpEdit) which support such scenario out-of-the-box. The idea is that LookUp editor has its own data source. When it is shown in the grid, it finds a value with from the grid cell in its data source (the key column determined by the LookUpEdit.Properties.ValueMember property) and shows the value from a field that selected as DisplayMember.
So if you put a table that has MaterialRequestContractor and MaterialRequestContractorId fields, and properly select DisplayMember and ValueMember, the DevExpress grid should work just like you described.
I have a window form that have two comboboxes and a datagridview which is bind with different datasource. one is for datagridview and another one is for both comboxes. This datagridview has two columns named like account1 and account2. these columns contains same value as in comboboxes.
Now my problem is that if i want to change combobox value then the current selected row value shuold change as in combox.
Suppose combobox contains values from 1 to 10. and currently selected value is 3 which is same in datagridview. If i change this combobox value from 3 to 7 then datagridview value should change from 3 to 7.
are u trying to do this
dataGridView1.Rows[0].Cells[0].Value = comboBox1.SelectedValue;//to get the selected text
dataGridView1.Rows[0].Cells[1].Value = comboBox1.SelectedIndex;//to get the selected text index
if not elaborate ur question clearly
If I have understood your query, you may follow as below:
dataGridView1[1, 2].Value = comboBox1.SelectedIndex.ToString();//Here, 1 represents row, 2 represents column.
dataGridView1.Rows[2].Cells[1].Value = comboBox1.SelectedIndex.ToString();
Both of the lines works same as per I know; just different syntax. You may use either.
Best of luck!
I have a DataSet, where in the first table and in the row i got a column called OrderStatus.
This field is a number between 0 and 3. What I want to achieve is when the data is loaded, I want to see the clear text of theOrderStatusin theComboBox` on my windows form.
Status 0 = Order in progress, 1 = Order is packaged, 2 = order
complete etc.
I have not found a way of binding the combobox and use a list of values to display the status in clear text.
Also when iIchange the status with the combo box I want it to reflect back to the dataset.
Use the DisplayMember and ValueMember property of the combo box.
The DisplayMember of a Combobox gets or sets a string that specifies the property of the data source whose contents you want to display. Here you want Order In Progress, Order is packaged etc..
The ValueMember property determines which value gets moved into the SelectedValue of the Combo Box. here you will set the corresponding ID values i.e. 0, 1, or 2 in your case
So, OnSelectionChanged you can get the SelectedValue i.e. 0, 1 or 2 which you bind earlier and update back the DataSet.
I am trying to bind a numeric field in an oracle table to a Datagridview - combo list in windows forms. With this what am trying to achieve is;
1 ) Based on the numeric value present in the number field, fetch ad display the respective string value in dataviewgridcombolist column.
2). When a new row is added in the datagrid view, the user may be allowed to select a listed (string) value in the combo list and the respective index value to be stored in the table back.
lots of thanks in advance..
Regards
Rithesh Krishnan
I've created a customized grid control. Where you can specify each column type the respective editing control will be displayed - for the combo box column a combo box field will be displayed, for the numeric column a numeric editor will be displayed.
While user adding a new row also user will get these editors. Usage of this class as follows
customDataGrid1.ColumnDataTypeMapping.Add("EmployeeName", CustomDataGrid.CustomDataGridColumnType.TEXT);
customDataGrid1.ColumnDataTypeMapping.Add("ManagerID", CustomDataGrid.CustomDataGridColumnType.COMBOBOX);
customDataGrid1.ColumnDataTypeMapping.Add("JoinDate", CustomDataGrid.CustomDataGridColumnType.DATE);
customDataGrid1.ColumnDataTypeMapping.Add("DateOfBirth", CustomDataGrid.CustomDataGridColumnType.DATETIME);
customDataGrid1.ColumnDataTypeMapping.Add("Salary", CustomDataGrid.CustomDataGridColumnType.NUMERIC);