I have a form showing the details of a client (various controls), along with their orders (in a DataGridView).
I am trying to add a ComboBox to let the user select one of the client's orders and display the items associated with it in a separate DataGridView.
However, I cannot work out which DataSource/DataBindings I need for either the ComboBox or the items DataGridView - please can anyone give me a few pointers?
Orders will be the data-source for the ComboBox - OrderId will be the Value field while Order Number or Order Date will be the text field. Items for that order will be data source for the items DataGridView. This grid needs to be bound in the combo-box's selection change event (set auto postback true for the combo box). Hope this helps.
Psuedo code for selection change event would be
protected void Orders_SelectedIndexChanged(object sender, EventArgs e)
{
var orderId = int.Parse(Orders.SelectedValue);
// Get items for this order from data store
var items = ...
// Bind with items grid
OrderItems.DataSource = items;
OrderItems.DataBind();
}
Orders is name of combo-box having orders while OrderItems is gridview to display items.
This seems to be already answered.
For example here:
How to get or set data from ComboBox in DataGridView
Or even better, just search for "DataGridView combobox" in StackOverflow and you will find many topics which cover every aspect of that problem.
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'm new to C# but not to programming. I've been toying around with VS, becoming more comfortable with the language and got stuck on a combobox bound to an int datatable column of an in-memory dataset.
I worked on a DataGridView and had no problems creating a DataGridViewComboBoxColumn and populating its dropdown list using a custom class comprised of Key:int and Desc:string. ValueMember = Key, DisplayMember = Desc. DataSource is left as null since the list is directly built into the ComboBox. Pick from the combobox and save changes works fine in the grid.
Then I moved to a ComboBox. Both the grid and the ComboBox have the exact same BindingSource. So yes they point to the same row at the same time.
I bind the ComboBox with:
cbCtrl.DataBindings.Add( "Text", myBindingSource, ColDBName );
Without populating the dropdown items of the ComboBox, I see the raw int value show up in the ComboBox. I change rows using the grid and the ComboBox value changes too. So it's good.
Then I populate the dropdown list of items in the ComboBox (the same as the grid's column tied to the same column) and it's trouble. The raw int value (eg 19) does not get matched to the proper "Key" item in the dropdown list of the ComboBox. I've tried DropDownStyle = DropDown & DropDownList but no luck with either. And I've looked for hours trying to find a fix. There are no events hooked up to the ComboBox. I expected automatic hookup of the raw value to the matching "Key" item in the ComboBox's dropdown list.
Is there something else to hook up? Frustrating since I expect something minor.
Any help would be Greatly appreciated. Thank you.
*Edit: The dropdown list actually comes from a populated BindingList<> and the BindingList<> is supplied as the ComboBox.DataSource.
I finally stumbled upon the answer:
cbCtrl.DataBindings.Add( "SelectedValue", myBindingSource, ColDBName );
That's all I needed. I got it from a great article
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial?msg=5325959#xx5325959xx
I'm loading a record to an aspx page. I have two comboxes. (AJAX comboboxes in this case) The second Loads based on the id from the first.
When I pass a key to the page in the query string I retrieve data into a class, then I populate the page fields from the class. When I set the carrier.selectedvalue = class.1value the selection shows correctly. When I set product.selectedvalue = class.2value the selection does not take. (The product has nothing selected)
If I drop down the list of the product combobox the correct data is loaded based on the the first combox (carrier).
I have tried two methods:
1) query product sql dataset (2nd dataset) based on selected value from the carrier combox
2) Load ALL products (2nd dataset) then FILTER the products based on the value from the carrier combox
Both methods load the 2nd combox with the values I need. Neither method helps me get the product combobox to show the selected value once the page renders.
Note that I'm doing all this in page_load
-Thanks in advance for looking.
The problem is that you are doing everything in Page_Load. Do things in the following order in the following events to prevent your issue:
Page_Init: Populate Carrier Items
Page_Init: Set Carrier selected value
Page_Load: Clear Items Product.Items.Clear()
Page_Load: Populate Product Items
Page_Load: Set Product Selected Items
See this guide for how to effectively use the Page events: http://attemptsatprogramming.blogspot.com/2011/03/practical-guide-to-aspnet-event-model.html
Gthompson83 put me on the right track. It was a databinding issue. I moved the set of the product combobx (the second combobox) to the databound event like so:
protected void cboProduct_DataBound(object sender, EventArgs e)
{
// Set the Product cbo
cboProduct.SelectedValue = c.Product_ID.ToString();
}
That is all it took. The class is still populated in the Page_Load event based on values from the query string. Once the DataBound event is fired i use the values I've placed in the class to set the value of the product combobox.
Some helpful info is here: Databinding events for data-bound controls
On the page linked above there is a good section on "Nested Data-Bound Controls".
Note that I did not follow that example completely... I did not do the databind on the second combobox programmatically... just catching the DataBound event was enough to allow me to set the selectedvalue.
Here is the situation:
I have populated a combo box with the names of divisions in my company and it is working fine. I go into edit mode and pull one record out of the data table so I bind all controls on the form with one record. I also populate this combo box with all divisions but want it to display the selected division. While I know how to display correct date in textbox controls, I do not know how to make combo box display only selected data. It displays the first record from the query which populates it. Any suggestions?
Thanks
Not clear if you are using ASP.NET or Windows Form. I am assuming Windows Form at the moment since it has an actual ComboBox control, while ASP.NET only has DropDownList (not counting the AJAX Control Toolkit).
ComboBox has a bunch of Selected... properties, i.e. SelectedIndex, SelectedItem, SelectedValue, SelectedText that you can manipulate (set) to show a certain item on the screen. So you can just do cbDivision.SelectedText = myRecord.Division (assuming Division in myRecord contains the same name as the one bound in the ComboBox.
for reference, see: this
I'm not sure I understood your question correctly. But I think you just want to display the selectedValue in the dropdown list populated from a list of values.
< asp:DropDownList DataTextField="SomeDecsription" DataValueField="SomeValue" ...... />
I apologise if this is not what you were asking for
I imported access database into c#, created form with a combo-box that list all customers, created a list-box to show address details of that customer(2 different tables), how do i bind that combo-box to the list-box...
the relationship is 1-many (customer(1) -- address(many))
Put the same source/DataSource/List in the 2 DataSource of your combo and list box dont forget to bind them, if you put some code i could help a bit more
i would do this on the ComboBox.SelectedIndexChanged event. rebind your ListBox.DataSource with the ComboBox.SelectedValue of the ComboBox.