Goal
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.
Current problem
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 :)
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
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
DataSource set to something that provides the key/value lookup, for example a SteelThicknesses datatable with two columns Disp and Val and has rows like "Thick",1 "Medium", 2 "Thin", 3,
DisplayMember set to a string of the column name from the lookup table that contains the text to show, for example "Disp",
ValueMember set to the string column name from the lookup that has the value that relates to the text to show, for example "Val" and
DataPropertyName set to the name of the property(column) in the other table that has the value to be decoded (one of the values in the Val column of the lookup table) (for example a Products table with a SteelThicknessID column).
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
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.
Related
or how can I populate the rows only from my datagridview since I want to assign a columnheader name on my datagridview instead of filling the database with all my data. I want to adjust the column height/width and such of each column. Thank you
I know why you put an image, but you should perhaps try to avoid it for future questions; people tend to prefer seeing code as text they can copypaste
You have your DGV set to autogenerate columns so it makes columns with a HeaderText that is the same name as the column. Just change the column HeaderText after the columns have been bound (binding happens when you set the DataSource property):
For example you can look the DGV column up by the datatable column it is bound to:
yourDataGridView.Columns.Cast(Of DataGridViewColumn)().First(Function(c) c.DataPropertyName = "ip").HeaderText = "IP"
Bound columns don't have a name, only an index position, so if you want to find them by name you'll have to look through the columncollection searching for eg where the column's DataPropertyName is "ip", which is what the LINQ above does. It's a bit awkward as a syntax because a DataGridViewColumnCollection doesn't do LINQ without being cast. It might be simpler to put all your mappings into a dictionary and use a loop:
Dim d = New Dictionary(Of String, String) From
{{"ip", "IP"}, {"aws", "AmazonWebServices"}, ... }
For Each dgvc as DataGridViewColumn in d
If d.ContainsKey dgvc.DataPropertyName Then d.HeaderText = d(dgvc.DataPropertyName)
Next dgvc
If you know "ip" column will always be first you can more simply:
yourDataGridView.Columns(0).HeaderText = "IP"
Insert data to DataSet..
Dim table As New DataTable
table.Columns.Add("Name")
table.Columns.Add("Score")
table.TableName = "Group1"
dataset.Tables.Add(table)
Combine with datagridview
dgv.DataSource = dataset.Tables("Group1")
There are two different situations
Set the column Collection in designer
Didn't set anything in designer
If I set the dgv column "Name" and "Score" in designer, the outcome will be like
Name Score Name Score
John 100
Jack 100
The duplicate columns appeared...
If I set nothing to the dgv, the outcome will be fine.
However, if there is no data in DataSet, the column header, "Name" and "Score", won't show up in dgv.
How do I solve this problem?
I would like to display the dataset in dgv even when the dataset has no data and the header still can be displayed.
I have created 4 datagridview columns in design mode, and I have bounded a datatable to datagridview's datasource, and its generates autocolumns (4 Columns)in gridview.
My doubt is If I create 4 datagrid columns in design mode, How can I place the values of bounded columns to default columns (created in design mode).
See the screenshot, first four columns are created in designmode, other 3 columns autogenerated, But here I dont know how can I correctly place the cells.
This is my first doubt, second is,
Is it possible to add a new row to the same datagridview (from 4 textbox values) ? if yes, How ?
I appreciate your help on this doubts
You need to bind predefined columns to the columns of the datasource.
Set DataPropertyName property of DataGridViewColumns to the name of the column in the datasource
Me.DataGridViewColumnOrderDetID.DataPropertyName = "ORDER_DET_ID"
Me.DataGridViewColumnOrderDetItem.DataPropertyName = "ORDER_DET_ITEM"
' etc.
This can be done in the designer too.
And set DataGridView.AutoGeneratedColumns = false.
Create and add new row to the datatable based on the values of textboxes
Dim newRow As DataRow = yourdatatable.NewRow()
With newRow
.SetField(Of Int32)("ORDER_DET_ID", Int32.Parse(Me.TextBoxOrderDetID.Text))
.SetField(Of String)("ORDER_DET_ITEM", Me.TextBoxOrderDetItem.Text)
' etc.
End If
yourdatatable.Rows.Addf(newRow)
Dear Sirs
I have a datagridview than bind to Table 1 .
so i have a DataGridViewComboBoxColumn that bind to table 2 .
Suppose table 2 contains [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t].
The DataGridViewComboBoxColumn must show only [a,b,c,d,e] For the first row and show only [f,g,h,i,j] For the Second row and show only [k,l,m,n,o,p,q,r,s,t] the third row.
Thanks a lot
A combobox in a grid column must contain all the same values for every row. However, you can filter the values so that the combobox appears to have different content.
I answered a similar questions here that demonstrates adding a filter to the datasource of the combobox:
http://social.msdn.microsoft.com/Forums/en-US/b23d9e8f-a00a-49ba-adf5-52d87c1b2890/parent-child-comboboxes-in-datagridview?forum=winformsdatacontrols
Dim Comp = From C In db.Table1 _
Select C.Completed, C.Taken, C.Namne
Datagridview1.DataSource = Comp
Am using the Entity Framework and Columns Completed and Taken are of bit Datatype. When the query results are displayed in the datagridview, these bit columns are returned as of ColumnType Textbox - so i get a Datagridview textbox column with true or false string values.
I want to display Completed and Taken as Checkbox columns (either ticked for True or un-ticked for false) but ofcourse i can't do this in EditColumn dialogue because the Datagridview is unbound.
how can i change this in code at runtime
Edit:
I just looked at your code again and I realised that what you actually mean is that the DGV is bound rather than unbound? Since you're binding it to Comp.
If I understand correctly, just create 3 columns in the DGV at design time, 1 DataGridViewTextBoxColumn and 2 DataGridViewCheckBoxColumn. Then set the DataPropertyName of each column (Completed or Taken for the checkbox columns etc).
If the DataPropertyNames are correct the data will appear in them (if they're not correct, you'll end up with 6 columns in total).