How to Show different data form a DataSource on DataGridviewComboboxColumn for each rows in DataGridView - vb.net

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

Related

DataGridViewComboboxCell display member not showing up

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.

Joining bounded datagridview columns to default column

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)

table get the cell from the row

I have one table with 3 rows and 3 columns. Now I want to add 2 row's 2nd columns cell's control. That means whether that is text or combox in the cell. How do I get the 2nd row's 2nd column and remove the compoenent dynamically in SWT JFace?
Do you use a TableViewer?
The SWT-way of getting to the item is indexed first by row, than by column.
Getting the text of the third column in the second row is done like this:
table.getItem(1).getText(2);
To display custom-controls, like a combobox you will have to either paint it manually or use SWT's TableEditor.
Also check out this tutorial: http://www.eclipse.org/articles/Article-Table-viewer/table_viewer.html

Multi-Column ComboBox in Excel VB

I have some ComboBoxes in a UserForm. One of the ComboBoxes is a multicolumn ComboBox with three columns and is dependent on another ComboBoxs' value.
Does anyone know WHY when I choose one of the lines in the multicolumn combobox shows only the first column value?
I want the value from the second column to be visible when I choose a line.
The Combox Property that set which column to display in the TextBox portion of the Combobox is TextColumn
Property values:
-1 = Display the first column whose width (set by ColumnWidths property) is > 0
0 = Display the value of ListIndex
1 = Display column 1
etc

Show bit Column as a checkbox column in unbound Datagridview

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).