Populate combo box in datagridview for new rows - vb.net

I have this code which i am using to populate a combo box in a datagridview
Dim dgvcc As DataGridViewComboBoxCell
dgvcc = DataGridView2.Rows(0).Cells(2)
dgvcc.Items.Add("comboitem1")
dgvcc.Items.Add("comboitem2")
it works fine but when a new row is added to the datagridview the combo box is not popuated with the data - i want it to be populated with the same data for every row

Instead of assigning the selections to an individual DataGridViewComboBoxCell (which would be only for that specific cell), assign the selections to the entire DataGridViewComboBoxColumn instead. Doing this will mean every cell in the column will share the same selection options.
With DirectCast(DataGridView2.Columns(2), DataGridViewComboBoxColumn)
.Items.Add("comboitem1")
.Items.Add("comboitem2")
End With
Note that I am assuming column 2 is your combo box column since that is what is used in your snippet.

Related

How to load items from combox into datagridview

How can I get items from combobox in to datagridview and when i click on a cell in datagridview it will lead me to edit so i'm thinking for edit when user clicks on a cell i will display its raw item in texbox and when user press enter it will change in in datagridview as well as in combox
how should i do this in vb.net
You will need to be more clear with your question.
But, for example if you have some items in your ComboBox you can copy them to DGV like this:
For Each item In ComboBox.Items 'loops over combobox
DataGridView.Rows.Add(item) 'adds a new row to datagridview and puts a value from combobox in it
Next

DataGridView ComboBox Cell error when closing form

I have a ComboBox column on my datagridview, the ComboBox is bound to a datasource for the values.
On Form Load I add rows and data to my DataGridView from a database table, here's how I add the value to the combobox column. This code is inside a loop, hence the rowindex variable.
Dim combo As DataGridViewComboBoxCell
combo = CType(Me.DataGridView1(3, rowindex), DataGridViewComboBoxCell)
combo.Value = sqldr("company_code")
It works fine for me, the combobox value is changed to what I had in my table and I can change the value of the cell using the combobox but if I try closing the form I get this error which just keeps repeating if I press Ok, I have to stop debugging just to exit the application.

How do I use ONE control source with MULTIPLE combo boxes from a USER FORM, in Access?

I am currently developing A quote sheet for work, I have 21 Combo boxes that are all populated by the same row source. The problems that I am having are When all my combo boxes are set to the same control source all the list values in the combo box are filtered and set to one value. For example combo box 1 is selected and prep is selected now the other 20 combo boxes are set to prep, and I can't select the other options. Now if I have no control source for my combo boxes than previous example works. If I select combo box 1 and set it to prep then I can select combo box 2,3,4... and put any value I want for each one. I want to be able to have each combo box with its own value.
The reason I am wanting the combo boxes to have the same control source is so when a combo box has a selected value it will add a new record to my table. So if 10 combo boxes have values selected then there will be 10 new rows added to my table when I click my add record button.
I am thinking a maybe having a loop do the work, but am not to familiar with with using vba to run sql??
You can't use the same control sources to generate multiple records on the same form. They must be unbound. At least, I don't know how to make that work the way you have it set up, but can still help.
Delete the control sources but keep the row sources.
You will need a query to save the values to your table:
INSERT INTO MyTableName ([MyFieldName]) VALUES ('[ComboboxValue]');
Copy this code and paste in query builder. Overwrite "MyTableName" and "MyFieldName" with the appropriate object names for where you want a single combobox value to be saved. It only adds one row at a time. [ComboboxValue] will be the parameter we use to pass each combo value to the query to execute.
Name the query whatever you want and save it. I will use the placeholder "MyQuery," make sure you update it!
Create a command button on your form, and in its On Click event, paste in this VBA code (you will need to update the combobox references where I use Me!ComboboxName):
Dim qdf As DAO.QueryDef
Dim combo As String
Set qdf = CurrentDb.QueryDefs("MyQuery")
combo = Me!Combobox1Name
qdf!ComboboxValue = combo
qdf.Execute
combo = Me!Combobox2Name
qdf!ComboboxValue = combo
qdf.Execute
'Copy those three lines for each combobox, adjust your combobox references accordingly,
'or use a loop
'End your procedure like this:
Set qdf = Nothing
If you are using a multi-column combobox, you may need to adjust the reference from Me!Combobox1Name to Me!Combobox1Name.Column(1).

How to make a reset button in excel

I am trying to make a reset button that will replace any value the user has selected with the value TOTAL inside a number of comboboxes. Using the record macro i selected the combobox but i can't figure out how to insert the value. The following code gives out the 424 error.
ActiveSheet.Shapes.Range(Array("ComboBox2")).Select.Value = TOTAL
the part that i added to the macro is the .Value=TOTAL
Anyone knows what i should do? Please take note that i don't want to clear the comboboxes; I want to give them a specific value.
In case that combo boxes are Form controls use OLEFormat.Object.ListIndex to select the item.
In case that the combo boxes are ActiveX controls use OLEFormat.Object.Object.ListIndex (note in this case the first item in the list has index 0).
Then iterate through all Combo-boxes you want to reset and set ListIndex to index of item "TOTAL". In this example the item "TOTAL" is on the first place in the list so for Form Combo-box (if used) ListIndex = 1 and for ActiveX Combo-box ListIndex = 0. HTH
(You are probably using ActiveX Combo-boxes, but in the example the older Form Combo-boxes are used as well just for the case).
Sub ResetToTotal2()
Dim combos
Dim combo
Dim comboObject
combos = Array("ComboBox1", "ComboBox2", "ComboBox3")
For Each combo In combos
Set comboObject = ActiveSheet.Shapes(combo).OLEFormat.Object
If TypeName(comboObject) = "DropDown" Then
comboObject.ListIndex = 1
Else
comboObject.Object.ListIndex = 0
End If
Next combo
End Sub

How to make custom Combo box control which have contain datagrid in vb.net

I want to make custom combo box which have contain datagrid and textbox too.
On clicks into combo box datagridview should be appear and selecting any row particular cell value of the datagridview added on the combo.
thanks in advance..
As your question is written I have no idea how to answer it, I can't figure out how you could sensibly show a DataGridView inside a ComboBox. I'm writing this answer with the assumption that you mean that the form should have a ComboBox that are "linked" to a DataGridView rather than actually contain it.
If so, all you need to do is to add the ComboBox and DataGridView to the form, make the DataGridView invisible. Then you handle the SelectedIndexChanged event for the ComboBox, in the handler, make the grid visibile, find the index of the row and column you want to show and write code like (code not tested so might not be correct):
grid.SelectedRows.Clear()
grid.FirstDisplayedScrollingRowIndex = rowIndex
grid.Rows(rowIndex).Cells(colIndex).Selected = True
grid.Show()