3 Replies - 9361 Views - Last Post: 12 October 2012 - 05:25 AM Rate Topic: -----

#1 Taledus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-October 12

Add data-bound ComboBox selection to data-bound DataGridView

Posted 11 October 2012 - 01:40 PM

I have some data bound controls on my form. I am restricting the user from inputting anything directly into the table and am handling this through code. The user can select an item from the combo box, hit the add button, then the items (ItemID/Item/Price) get populated into the data grid. Here is what I am dealing with:
Spoiler


The combobox is correctly displaying the items stored in the DB.

Button click event:
Me.MyDataSet.OrdersOrderItems.Rows.Add(???)



I have no idea what to put here '???' in order to populate a new row in the datagrid with the appropriate data from the table.

Is This A Good Question/Topic? 0
  • +

Replies To: Add data-bound ComboBox selection to data-bound DataGridView

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Add data-bound ComboBox selection to data-bound DataGridView

Posted 11 October 2012 - 02:20 PM

With Me.MyDataSet.OrdersOrderItems.Rows.Add(???) you are trying to add new row to table, not to DGV. DGV should be updated with bindingsource .ResetBindings.

If you want to add new row to data table, you need to specify values for each column in it (unless it allows NULL values, or perhaps for AutoIncrement(I should check this, not sure)).

First you define new row: Dim newOrdersRow As DataRow = MyDataSet.Tables("OrdersOrderItems").NewRow()
Then you set all column values, like:
newOrdersRow("OrderID") = 5
newOrdersRow("ItemID") = 15
...



...and only then you add row with: MyDataSet.Tables("OrdersOrderItems").Rows.Add(newOrdersRow)

This post has been edited by lucky3: 11 October 2012 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 Taledus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-October 12

Re: Add data-bound ComboBox selection to data-bound DataGridView

Posted 11 October 2012 - 02:56 PM

The OrderID is already determined as it is bound to the current order, so if I had a binding navigator attached to it, the ID would already be generated. Since I already know that the current list of OrdersOrderItems is for the current order, I do not have that column listed in the datagrid.

However, I am not sure how to determine the row for the combo box's selection to be able to get the other cells data. Since it is only showing the list of Items in OrderItems table, I am unable to access the selected item's index. (if that makes sense). Right now I have 3 items in there so it is not too difficult to figure out where the index is at...but it is likely that there will be several dozen items in this list.

The user see's the list of Items that are going with the current order, and they can add whatever they want from the combobox (items list) by clicking the button. However, I am open to suggestions of a better way to go about this...I might be going about this the wrong way :/
Was This Post Helpful? 0
  • +
  • -

#4 Taledus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-October 12

Re: Add data-bound ComboBox selection to data-bound DataGridView

Posted 12 October 2012 - 05:25 AM

I just wanted to post this here for anyone else who came across this in a search:

Dim rowIndex As Integer = OrderItemComboBox.SelectedIndex

Dim id As Integer = Me.MyDataSet.OrderItems.Rows(rowIndex).Item(0)

Dim itm As Integer = Me.MyDataSet.OrderItems.Rows(rowIndex).Item(2)

Me.MyDataSet.OrdersOrderItems.Rows.Add(OrderIDTextBox.Text, id, itm, nudOrdersOrderItemQty.Value)



Since this would be a single user app, and the indexes of my item list change in a separate window and not when creating an order, the Item table and the combobox are going to have the same index...meaning I can just lookup the Item by it's selected index in the combobox.


However, I have run into yet another issue. The OrdersOrderItem table has ExtdPrice, and when I put the expression (Quantity * UnitPrice) through the Data Source Designer for that column then run my program I get the following error: The column mapping from SourceColumn 'TotalPrice' failed because the DataColumn 'TotalPrice' is a computed column.

I can then create another column on the Designer window that has no 'Source', use the same expression, and this will then work but it will not save in the DB. Likewise, I can handle this issue in the previous solution about the indexes, and just add this instead:
Me.MyDataSet.OrdersOrderItems.Rows.Add(OrderIDTextBox.Text, id, itm, nudOrdersOrderItemQty.Value, (itm * nudOrdersOrderItemQty.Value))


I know, I just posted my own solution to my own problem before someone else could take a look, but that isn't the point. The reason I posted that was I do not understand what about being a 'computed column' screws up the mapping of the 'TotalPrice", and was hoping someone might have a better idea.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1