DataGridView combo-boxes and primary keys - vb.net

What's the best practise way to handle a primary key in an unbound datagrid view combo-box column?
I want to display a meaningful value but store only the primary key (much like Access combo-boxes). I achieve this with a regular combo-box by adding an object with two properties into the items collection rather than a plain string. I then retrieve this by casting the selected index value of the combo-box back into it's object form and then retrieving the properties. This works very well but I am unable to replicate the technique with a datagridview combo as I can't seem to access the items collection. I only seem to be able to retrieve a string value back from the grid although I can add the object as normal when creating the column.
Hope this makes sense...

RESOLVED - Set the combo datasource to a collection class and use the ValueMember and DisplayMember properties in the same way as when binding to a datasource. Works a treat.

Related

vb.net create tabpages and datagridview from list exported from SQL

Just after some ideas on the best way to create additional tabpages dynamically within a tabcontrol and also to then add the relevant data on each tab with a datagridview.
The tabcontrol will always exist, but I'm not sure whether to initially create the additional tabpages first by identifying the distinct data in SQL, and then run a further script to extract the data, which then needs to be added to the datagridview, or whether I can use vb.net to create the distinct tabpages and add the data from one table, as below.
I can add another tabpage dynamically but not sure the best way to do it for a list, that is extracted from SQL.
Hope that makes sense. Thanks
TabCode
TabName
DataName
DataType
1
One
Test Name
Text
1
One
Test Date
Date
2
Two
Test Value
Value
I've managed to create new tabpages, but only single instances as not sure how to create them from a list extracted from SQL. I'm then struggling to add the data to the relevant datagridview, which I envisage would be created with a name linked to the tabpage name.

Set vb.net combobox SelectedIndex by value

I am working on a tool that saves data into a database to later retrieve it and display in the same form
I save data into the database using integer id's
In order to set combobox values I have tried the following
frm.statusComboBox.SelectedValue = initiativeDataGridView.SelectedRows(0).Cells(initiativeDataGridView.Columns("initiativeStatus_Id").Index()).Value
I understand that I am in the wrong, since I need to set the combobox selectedIndex and not the selectedValue. So I guess my main question is how do I identify the combobox index for a particular value member?
Try using ComboBox.SelectedItem instead, but you should add the item as well, so it appears in the dropdownlist. Working with Indexes of ComboBox isn't the best idea, because they can change as you sort your ComboBox, delete or add items.

How to find all items in a bindinglist that have been changed

I have an OrderItems class that inherits BindingList. It stores a list of Order lines. These are presented in a DataGridView. The user can directly access the DGV and update values of each row and add new row.
When the user uses a "Save" button, I want to find all the rows that have changed, so I can then process them. How can I do that?
One idea I've had is to when instantiating the OrderItem Object, I keep another list in the OrderItems object to store original values, then iterate though the bindingList comparing against the original values in the other list? Seems a bit wasteful and intensive but there'll be only 10 or so items in the binding list.
Or should I have another field in the bindingList Object to store a flag that I'd have to set when a user changes a value in one of the rows cells (if so what event would be the best?), then iterate though the bindinglist, look for items with the flag set and just process them?
Its been a bit of a struggle understanding the DGV/Binding process, so I'd appreciate some guidance. I have looked online but most guides seem to deal with datatables/views and C#

VBA access and forms

I created a database in Access and there are some type of records in some tables that requires a particular inserting, so I decided to use VBA to handle this.
The problem is that if i create a form with some controls which i want to refer and use their values as criteria for queries, the form is still a way to insert data. So the query works but the data i inserted are added directly from the form too, creating duplicates.
The question is, is there a way to create a form that has controls only for text input but does nothing to record , leaving inserting, deleting , updating all to queries in VBA?
I tried to put "no" on propriety "add records" in the form but it gets totally blank with no controls.
Your form must be unbound, i.e. its RecordSource must be empty.
Your form can be bound or not bound to a table / query.
This means that the controls on your form may be bound to a field of that table / query.
But you can also have controls in the same form which are not bound.
Example:
You can make a form in which the body has a list view of the records of the table. In this section the controls would be bound to a field.
In the header of the table instead you may have controls that are not bound and that could be used either to filter the records shown or to add new records. You may want to add records this way rather than let users insert data directly in order to add checks or do any other kind of processing before actually adding the data in a new record.

What are different ways in VB Dot Net to populate DataGridView Grid using SQL Query?

I want to populate a DataGridView Grid in my Form with Data from SQL Query or from some Table form Database. I want to know various possible ways to populate the DataGridView Grid.
Thanks in advance.
There are basically 3 ways to display data in a DataGridView
Create the rows manually in a loop: it's very inefficient if you have a lot of data
Use the DataGridView's virtual mode: the DGV only creates as many rows as can be displayed, and
dynamically changes their contents when the user scrolls. You need
to handle the CellValueNeeded event to provide the required data to
the DGV
Use databinding: that's by far the easiest way. You just fill a
DataTable with the data from the database using a DbDataAdapter, and
you assign this DataTable to the DGV's DataSource property. The DGV
can automatically create the columns (AutoGenerateColumns = true),
or you can create them manually (you must set the DataPropertyName
of the column to the name of the field you want to display). In
databound mode, the DGV works like in virtual mode except that it
takes care of fetching the data from the datasource, so you don't
have anything to do. It's very efficient even for a large number of
rows
Link.