vb.net / DataGridView / ComboBoxCell? - vb.net

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"})

Related

Setting focus to another control prevents current control value from changing in VB.NET

I have a ComboBox in a WinForms app written in VB.NET. In the .SelectionChangeCommitted event I want to change the focus to a different specific control to assist user workflow. However when I do that, the change is not saved on the initial ComboBox and the value & index are reverted to the original values.
I've used both myControl.Focus and myControl.Select
The Combobox is setup like this:
With ChoosePartType
.DisplayMember = "PartName"
.DataSource = GetTable(qry) 'This custom function returns a DataTable with fields PartNum and PartName
.ValueMember = "PartNum"
.SelectedIndex = -1
End With
I assume something in the changing focus is short-circuiting the property change. Is there a way to force that to happen before I change focus?
Note: seems like a different issue from WInforms Combobox SelectionChangeCommitted event doesn't always change SelectedValue
Similar to this but I don't use databindings: Combobox DataBinding Bug - Won't write value if programmatically losing focus
Try using:
ChoosePartType.SelectedItem.Row("PartNum")
Assuming that this is the first column of the control's datasource, this could be also written as:
ChoosePartType.SelectedItem.Row(0)

Datagridview menuitem - get data of datagridview row

I am using a Datagridview, and if I right click on a row in the Datagridview I am catching the Datagridview MouseClick handler. Inside this handler I create a menuItem, if I click one entry in this menuItem I want to execute some code, and this code should get some data from the row I have selected in the Datagridview. How can I do this?
I am using this kind of code to create menuItem entrys:
Dim firefox As New MenuItem("Firefox", AddressOf contextMenu_ItemClicked)
firefox.Tag = "firefox"
Another question, do I need to add this tag, or am I also able to read out the name of the menuitem in the contextMenu_ItemClicked function?
Thanks!

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.

Changing styling of DataGridViewComboBoxCell from DropDownList to DropDown?

(This question is narrower in scope than this question I asked earlier.)
Say I have a DataGridViewComboBoxColumn, and want to switch the style of the ComboBox control between DropDownList and DropDown (mainly for the text field editing capability). I'd like to do this on a row-by-row basis (at the DataGridViewComboBoxCell level).
How would I do that programatically?
Or maybe a different way of asking: How do I access the ComboBox control given an object of type DataGridViewComboBoxCell?
(I'm in VB 2005.)
Thanks as always.
Not sure if you still need an answer, but i have a question that covers similar ground: Detect which column is showing an editing control in a datagridview
i use something like this line to get the combobox out of the DataGridView (DGV) in my DataGridView's CellValidating event:
Dim comboBox As DataGridViewComboBoxCell = DGV.Item(e.ColumnIndex, e.RowIndex)
later i use this line to get change the DropDownList ComboBoxCell to a DropDown:
cb.DropDownStyle = ComboBoxStyle.DropDown
For mine to work i had to make sure the 'cb' was of the ComboBox type, i dont remember if i was able to get it working well if the combobox was of the DataGridViewComboBoxCell type.

Setting the focus in a datagridview in windows form

I have a datagridview that accepts a list(of myObject) as a datasource. I want to add a new row to the datagrid to add to the database. I get this done by getting the list... adding a blank myObject to the list and then reseting the datasource. I now want to set the focus to the second cell in the new row.
To CLARIFY i am trying to set the focus
You can set the focus to a specific cell in a row but only if the SelectionMode on the DataGridView is set to CellSelect. If it is, simply do the following:
dataGridView.Rows[rowNumber].Cells[columnNumber].Selected = true;
In WinForms, you should be able to set the
Me.dataEvidence.SelectedRows
property to the row you want selected.
In Visual Studio 2012 (vb.NET Framework 4.50), you can set the focus on any desired cell of a DataGridView control.
Try This:
Sub Whatever()
' all above code
DataGridView1.Focus()
DataGridView1.CurrentCell = DataGridView1.Rows(x).Cells(y) 'x is your desired row number, y is your desired column number
' all below code
End Sub
Okay, that works for me. I hope that it works for you, too.