Reputation: 4938
A datagridview of SteelTypes has a column that has been bound to a dataview (SteelThicknesses). I can select the data and set it correctly. However I cannot load the same information. My datagridview comboboxcell contains the value and editted formatted text but I cannot set the display member information.
I have all of my textbox columns loading correctly except the combobox column. The variable of cbCol is set correctly and the EditedFormattedValue and FormattedValue contain the value I want! However the displayMember does not get replicated into the datagridviewcombobox cell.
I am trying to display a "3" into the Épaisseur (thickness) column via setting its value to a primary key (PK_SteelThickness):
See the results below. Everything except my comboboxcell is populated:
Appreciate the help in advance, this has gotten me crazy :)
Upvotes: 0
Views: 1172
Reputation: 4938
This is an odd behavior but I found that changing my loading method calls from the new() to the form Load() made it work...
So I changed my initial code from this:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
LoadSteelPlates()
LoadPlateQuotes()
End Sub
To this:
Private Sub frmQuotePads_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadSteelPlates()
LoadPlateQuotes()
End Sub
This is the only code that was modified and now it works.
Upvotes: 0
Reputation: 74605
It seems like you're making a lot of work for yourself here. Datagridcombobox can be used to decode a value simply by having its
"Thick",1
"Medium", 2
"Thin", 3
, The datagridview is bound to the products datatable, the combo will find e.g. 3 in the steelthicknessid column, it will look 3 up in the Val column of the steelthicknesses table and show the text it finds in the Disp column of that row from steelthickesses.if the user changes the value shown by dropping the combo list and picking Thick, it will work the reverse and take 1 from the Val column and update the products table with the new value 1 for steelthicknessid. If you don't want that, make the column or datagridview read only
For a more involved discussion see my answer in DataGridView Loading From LINQ
Upvotes: 3
Reputation: 54417
You don't set the text. You set the underlying ID. The idea is that you bind the column to a parent table and you bind the grid to a child table that has a foreign key to that parent table. You set the foreign key value in the grid cell and the corresponding text value from the parent table gets displayed.
As an example, let's say that you have a Handedness
table like so:
Id Name 1 Right 2 Left 3 Ambidextrous
and you have a Person
table like so:
Id Name HandednessId 1 Peter 2 2 Paul 1 3 Mary 3
In your grid you would create a DataGridViewComboBoxColumn
and set its DataPropertyName
property to "HandednessId". When you then bound your Person
table to the grid, the HandednessId
column would bind to the combo box column. You would bind your Handedness
table to the column and set the DisplayMember
and ValueMember
to "Name" and "Id" respectively. The grid would then display "Left", "Right" and "Ambidextrous" for "Peter", "Paul" and "Mary" respectively. If you wanted to make Peter ambidextrous, you would set the HandednessId
cell Value
to 3 and then it would display "Ambidextrous".
See this for more information:
Adding a ComboBox Column to a DataGridView
Upvotes: 3