DataGridView ComboBox Cell error when closing form - vb.net

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.

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

Setting ComboBox RowSource property to query in "GotFocus()" method leaves blank values in ComboBox Access VBA

I want to populate my ComboBox in an Access form using VBA based on data from another table. Previously to do this I did the following:
In the Property Sheet -> Data tab I filled out Row Source and Row Source Type fields with this information:
Now whenever I clicked on the dropdown for my combobox, it would populate the dropdown list with all of the names from t_people table.
This limited me however to when data changed in the t_people's name column. In order to get an updated list, I must close the form and re-open it so that the query runs again. I have limited the access to this Access file so that the user is only presented with x number of forms, and cannot close/open them or others.
My solution is to remove the query on the form load, and instead run the query every time the combobox gains focus, has a click event or something of the same sorts. I did this with the following event in VBA:
'Run when the "name" combobox gains focus
Private Sub nameCb_GotFocus()
[nameCb].RowSource = "SELECT name FROM t_people"
End Sub
I have set breakpoints and this code does run. However, the the combobox is not populated after it does. How can I get the combobox to populate with the values from a query for each time the combobox gains focus?
Set the RowSource in design and add a .Requery when entering the control.
Private Sub nameCb_Enter()
nameCb.Requery
End Sub

Populate combo box in datagridview for new rows

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.

Set focus on ComboBox (ActiveX Control) after code execution

I have an Excel file with a combobox (name = "Combobox1"). After running a script (basically pasting the selected value in "the next row" of a column) I want the focus to be reset on the combobox at the end of the script, so doing allowing me to type the next entry in the Combobox without having to click on the ComboBox text field first.
This does the job in Excel 2013 but I would like to have it working in 2007 as well:
Combobox1.Activate
Anyone any idea?
Or:
I can replace the combobox with an in-cell dropdown list (data validation) and the same data validation as the one I have in the combobox at the moment, but then I have another issue:
For a ComboBox you can choose to have the dropdown list active, but for an in-cell data validation that is not the case, at least not if you want to be able to type in the cell after the list is shown with ALT+UP or
Application.SendKeys "%{UP}"
Any idea here?
If this combobox is on a worksheet and not a userform, then "Combobox1.select" should return the focus to the combobox.

vb.net / DataGridView / ComboBoxCell?

I am using vb.net 2010 and winforms and DataGridView.
The DataGridView has a DataGridViewComboBox column. When I show the form with the DGV it shows this and empty grid but the column that contains the ComboBox shows the first item on the dropdown list.
How can I have the ComboBox display nothing until it is clicked on and selected?
Try setting the combobox selectedindex property to -1 when you initialize it. That might fix your problem, but when I do the same thing that you described, mine doesn't show any values in the combobox until I click on it. Here are the steps I took:
1. create a datagridview control.
2. right click on control and add column.
3. add DataGridViewComboBoxColumn
4. right click on control and edit columns.
5. Click on the button for "Items (Collection)".
6. Add some items
Now your control should behave how you are asking. It works fine when I run it. If it doesn't it may be a VS2010 bug since I'm running VS2008.
Edit:
When you add your items in code, just set the combobox value to Nothing:
Dim cboBrand As New DataGridViewComboBoxColumn
With cboBrand
.HeaderText = "Brand"
.Name = "Brand"
.Width = 300
.Items.Add("item1")
.Items.Add("item2")
.Items.Add("item3")
End With
Me.DataGridView1.Columns.Insert(0, cboBrand)
DataGridView1.Rows.Insert(0, New Object() {Nothing})
or if you want to set an initial value, do it like this:
DataGridView1.Rows.Insert(0, New Object() {"item2"})