datagrid view combobox item selection vb.net - vb.net

I have a datagridview which I have furnished using the following code...
The trouble I have though is with the combobox column.
First off how can I have a preset value already selected in the combobox.
Secondly When I run my code I have to click the combobox's twice how can I change this to only having to click them once?
'CREATE DATAGRIVIEW 1 COLUMN LAYOUT...S
DataGridView1.ColumnCount = 8
DataGridView1.Columns(0).Name = "QTY"
DataGridView1.Columns(1).Name = "H"
DataGridView1.Columns(2).Name = "L"
DataGridView1.Columns(3).Name = "W"
DataGridView1.Columns(4).Name = "ANG 1"
DataGridView1.Columns(5).Name = "ANG 2"
DataGridView1.Columns(6).Name = "MAT"
DataGridView1.Columns(7).Name = "THK"
Dim ComboBoxColumn As New DataGridViewComboBoxColumn()
ComboBoxColumn.HeaderText = "TYPE"
ComboBoxColumn.Name = "ComboBoxColumn"
ComboBoxColumn.MaxDropDownItems = 5
ComboBoxColumn.Items.Add("NON")
ComboBoxColumn.Items.Add("SNG")
ComboBoxColumn.Items.Add("PER")
ComboBoxColumn.Items.Add("PAR")
ComboBoxColumn.Items.Add("AXS")
DataGridView1.Columns.Insert(8, ComboBoxColumn)
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "IDW"
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(9, checkBoxColumn)
'CREATE DATAGRIVIEW 1 COLUMN LAYOUT...E

If you mean a defaultvalue for the combobox:
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If (e.ColumnIndex = 8) Then ' your combo column index
e.Value = "PER"
End If
End Sub

The selection in the combo box reflects the Value of the cell. If you want something to be selected then you have to set the Value of the cell accordingly.
Your second issue, which is unrelated and should therefore have been asked in an unrelated post, is as a result of the EditMode of the grid. Change that to the appropriate value for the behaviour you want.

Related

Changing the format of all cells in a datagridview to combobox

I have a few data tables. I loop through the number of datatables that I have. In the loop I create a DataGridView with the datasource being the given Data Table at that index. How can I then change the columns to be of type Combobox? I know how to do this on the designer but what about programatically? I've tried:
For each column as DataGridViewColumn in table.Columns
column.CellType = DataGridViewComboBoxColumn
Next
Obviously this doesn't work. CellType is a read only property and I cannot simply set it equal to a class type. I know this is probably simple, I know I am stupid. Please help.
You don't need to define an entire column as a combo box. See my example where you have a definition for a combobox with the same value for every rows. I also included a way for you to only replace a single cell with a combobox. See how the "people" column is a textboxcolumn where I put a combobox instead. It also shows you how to assign values and set the default value. It's the same concept for a checkbox column/cell.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewCol As New DataGridViewComboBoxColumn
DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "ID", .HeaderText = "ID"})
With NewCol
.Name = "ComboBoxCol"
.HeaderText = "Language"
.Items.Add("VB.NET")
.Items.Add("C#")
.Items.Add("F#")
.DefaultCellStyle.NullValue = .Items(0)
End With
DataGridView1.Columns.Add(NewCol)
DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "PeopleCol", .HeaderText = "People"})
Dim xRow As String() = {"1"}
DataGridView1.Rows.Add(xRow)
DataGridView1.Rows.Add({"2"})
DataGridView1(1, 0).Value = "C#"
Dim xPeople1 As New DataGridViewComboBoxCell
With xPeople1
.Items.Add("Santa")
.Items.Add("Satan")
.Value = .Items(0)
End With
DataGridView1(2, 0) = xPeople1
Dim xPeople2 As New DataGridViewComboBoxCell
With xPeople2
.Items.Add("God")
.Items.Add("Allah")
.Value = .Items(1)
End With
DataGridView1(2, 1) = xPeople2
End Sub
End Class

VB.NET How to add Event Handler to ComboBoxColumn located in programmatically created controls?

I've got part of my code that creates a tab in a tabcontrol, and then fills it with a datagridview which contains a couple columns that are DataGridViewComboBoxColumn.
It looks like this:
Private Sub NewTabPage()
Dim TabPageCount As Integer = RacerOrderTAB.TabPages.Count
RacerOrderTAB.TabPages.Add(TeamNames(TabPageCount)) 'teamnames() is an array of team names
Dim CurrentTabPage = RacerOrderTAB.TabPages(TabPageCount)
Dim GridToAdd As New DataGridView
GridToAdd.Size = CurrentTabPage.Size
GridToAdd.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
GridToAdd.Location = New Point(CurrentTabPage.Location.X, CurrentTabPage.Location.Y)
GridToAdd.Columns.Add("ShiftCOL", "Shift Name")
GridToAdd.Name = "grid_" & CurrentTabPage.Text
For y As Integer = 1 To ShiftSetup.racerspershift 'add extra column for each racer in shift
Dim cmb As New DataGridViewComboBoxColumn
cmb.HeaderText = "Racer" & y
cmb.Name = "Racer_" & y
cmb.MaxDropDownItems = AmountOfRacers
cmb.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
GridToAdd.Columns.Add(cmb)
Next
RacerOrderTAB.TabPages(TabPageCount).Controls.Add(GridToAdd)
End Sub
But I've been having difficulty in adding an eventhandler for the comboboxes. What I want to happen is that when a combobox is clicked and opened, I populate it with the items I want.
I managed to vaguely get it working by adding:
AddHandler GridToAdd.EditingControlShowing, AddressOf <sub name>
but then have been unable to figure out which combobox was clicked, and how to populate it. It's also been requiring like four clicks before the drop list will appear. I'm only slightly very confused.
Thanks for any advice; these DataGridViewComboBoxColumns [deep breath] have been confusing me a lot!
It may be a little hacky but it should do what you are asking… hopefully. I created two List(Of String) variables. AllRacers contains all the racers… i.e. all the names we want to appear in a combo box such that there is no other combo box on that row that has an item selected. These names are what all combo boxes on all rows would initially contain in the selectable items list.
The other List(Of String) UsedRacers contains a list of all the “comboboxes” on the current row that have selected items. Each time a cell value is changed and it is one of the “combobox” column cells, then UsedRacers is cleared/updated to reflect the added/changed selected item on the current row.
When a “comboBox” cell value is changed, SetUsedRacersForRow is called…
Private Sub SetUsedRacersForRow(rowIndex As Int16)
UsedRacers.Clear()
Dim curValue = ""
For i = 1 To racersPerShift
If (Not (dgvRacers.Rows(rowIndex).Cells(i).Value Is Nothing)) Then
curValue = dgvRacers.Rows(rowIndex).Cells(i).Value.ToString()
If (Not (String.IsNullOrEmpty(curValue))) Then
UsedRacers.Add(curValue)
End If
End If
Next
End Sub
The code above loops through all the “combobox” cells in the given row and if a “combobox” cell has something selected, the selected value is added to the UsedRacers list.
Now that the selected items for all the “comboboxes” in that row are in the UsedRacers list, we can now loop through each “combobox” cell in that row and set the proper list of names. To help, a method is created that returns a DataGridViewComboBoxCell such that the names in the current UsedRacers list will NOT be in the DataGridViewComboBoxCell’s list of selectable names.
The only issue here would be with cells that currently have an item selected. Each “combobox” cell with an item selected will uniquely need to have its selected item in its list of items. To remedy this, a check is needed to see if the “combobox” cell contains a value. If the “combobox” cell DOES contain a selected value, then this value is also contained in the UsedRacers list. Since THIS cell is the cell that is in the UseRacers list… then we need to ADD this value to this cells items list. Otherwise, we would not be able to display the unique selection.
To keep the UsedRacers list consistent, we need to add this item directly to the individual “combobox” cell and not remove or alter the UsedRacers list as this will be used for the other “combobox” cells. In other words… whatever value is selected in a combo box, we need to make sure it is one of the items in the “combobox’s” list of selectable items. I hope that makes sense.
This can all be done in the DataGridViews CellChanged event.
Private Sub dgvRacers_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvRacers.CellValueChanged
If (e.ColumnIndex >= 1 And e.ColumnIndex <= racersPerShift) Then
SetUsedRacersForRow(e.RowIndex)
For i = 1 To racersPerShift
Dim newCell As DataGridViewComboBoxCell = GetCurrentComboBoxCell()
If (Not (dgvRacers.Rows(e.RowIndex).Cells(i).Value Is Nothing)) Then
Dim curValue = dgvRacers.Rows(e.RowIndex).Cells(i).Value.ToString()
newCell.Items.Add(curValue)
newCell.Value = curValue
End If
dgvRacers.Rows(e.RowIndex).Cells(i) = newCell
Next
End If
End Sub
In the code above, a method GetCurrentComboBoxCell (below) returns a DataGridViewComboBoxCell such that the items in the combo boxes list of items does not contain any items that are in the UsedRacers list. Because of this, a check is needed (above) to see if the cell already contains a value. NOTE: the DataGridViewComboBoxCell returned will always contain a “blank” empty item. This is necessary to allow the user to “De-Select” any currently selected value and then make the “De-Selected” item available to the other combo box cells.
Public Function GetCurrentComboBoxCell() As DataGridViewComboBoxCell
Dim newComboCell = New DataGridViewComboBoxCell()
newComboCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
newComboCell.FlatStyle = FlatStyle.Flat
newComboCell.Items.Add("")
For Each curRacer In AllRacers
If (Not UsedRacers.Contains(curRacer)) Then
newComboCell.Items.Add(curRacer)
End If
Next
Return newComboCell
End Function
Finally, putting all this together…
Dim racersInShift = 3
Dim AllRacers As List(Of String) = New List(Of String) From {"John", "Bobby", "Trent", "Josh", "Chapman", "Henry", "George", "Marvin"}
'Dim racersPerShift As Int16 = AllRacers.Count '<-- should be MAX value
Dim racersPerShift As Int16 = 4
Dim UsedRacers = New List(Of String)
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BuildGrid()
End Sub
Private Sub BuildGrid()
dgvRacers.Size = New Size(800, 200)
dgvRacers.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
'dgvRacers.Location = New Point(50, 200)
dgvRacers.Columns.Add("ShiftCOL", "Shift Name")
dgvRacers.Name = "RacersDGV"
dgvRacers.EditMode = DataGridViewEditMode.EditOnEnter
dgvRacers.AllowUserToAddRows = False
AddRacerColumns()
AddRacerRows()
End Sub
Private Sub AddRacerColumns()
Dim newColumn As DataGridViewComboBoxColumn
For i As Integer = 1 To racersPerShift
newColumn = GetNewComboBoxColumn("Racer" & i, "Racer " & i)
dgvRacers.Columns.Add(newColumn)
Next
End Sub
Private Sub AddRacerRows()
For i As Integer = 1 To racersInShift
Dim row As New DataGridViewRow
dgvRacers.Rows.Add(row)
Next
End Sub
Private Sub dgvRacers_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs)
‘See code above
End Sub
Private Sub SetUsedRacersForRow(rowIndex As Int16)
‘See code above
End Sub
Public Function GetCurrentComboBoxCell() As DataGridViewComboBoxCell
‘See code above
End Function
‘Lastly a method to set a whole `DataGridviewComboBoxColumn` which is used to initialize all the combo box columns
Public Function GetNewComboBoxColumn(colName As String, colHeader As String) As DataGridViewComboBoxColumn
Dim newComboCol = New DataGridViewComboBoxColumn()
newComboCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
newComboCol.FlatStyle = FlatStyle.Flat
newComboCol.Items.Add("")
newComboCol.HeaderText = colHeader
newComboCol.Name = colName
For Each curRacer In AllRacers
newComboCol.Items.Add(curRacer)
Next
Return newComboCol
End Function
I hope this helps, I am guessing there is an easier way to do this.

VB Combobox Index Change on datagridview mouse click row select

I am trying to set my textbox fields to the row column values when the row in the datagridview is double clicked. I have figured out how to populate my textbox fields, however, one of my fields in a combobox, and I can't seem to select the dropdown value according to selected row's column value. This is what I have currently.
Private Sub dgvCustomers_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles dgvCustomers.MouseDoubleClick
Dim row As DataGridViewRow = dgvCustomers.SelectedRows(0)
tbMasterContactID.Text = dgvCustomers.SelectedRows(0).Cells(0).Value.ToString
tbCompany.Text = dgvCustomers.SelectedRows(0).Cells(1).Value.ToString
tbContact.Text = dgvCustomers.SelectedRows(0).Cells(2).Value.ToString
tbAddress1.Text = dgvCustomers.SelectedRows(0).Cells(3).Value.ToString
tbAddress2.Text = dgvCustomers.SelectedRows(0).Cells(4).Value.ToString
tbCity.Text = dgvCustomers.SelectedRows(0).Cells(5).Value.ToString
tbState.Text = dgvCustomers.SelectedRows(0).Cells(6).Value.ToString
tbCountry.Text = dgvCustomers.SelectedRows(0).Cells(7).Value.ToString
tbZip.Text = dgvCustomers.SelectedRows(0).Cells(8).Value.ToString
tbZip4.Text = dgvCustomers.SelectedRows(0).Cells(9).Value.ToString
tbEmail.Text = dgvCustomers.SelectedRows(0).Cells(10).Value.ToString
tbPhone.Text = dgvCustomers.SelectedRows(0).Cells(11).Value.ToString
tbStatus.SelectedIndex = tbStatus.FindString(dgvCustomers.SelectedRows(0).Cells(12).Value.ToString)
End Sub

Changing double clicking activation on Combo box cell to single click?

I have a setup in my code where there is a datagridview. For each row I have a combo box cell that I have a separate combo box cell since I want a different selection of items for each cell.
Problem : The cell only drops down when the arrow is double clicked. How can I change the cell formatting, or possibly a cell click event, so that the cell response to just one click?
Here's my cell creation code. Frankly, I didn't start any other code since I didn't know what event to touch or call. Is there a property I can edit?
Code:
'add items to combobox list
Dim comboCell As New DataGridViewComboBoxCell
comboCell.FlatStyle = FlatStyle.Flat
Dim resolutionList As New List(Of cmbStruct)
Dim currentResIndex As Integer = 0
'create list of resolutions
For j As Integer = 0 To resolutions.Length - 1
Dim resClass As New cmbStruct
resClass.Name = resolutions(j)
resClass.ID = resolutions(j)
resolutionList.Add(resClass)
comboCell.Items.Add(resolutions(j))
Next
'set combocell values
comboCell.DisplayMember = "Name"
comboCell.ValueMember = "ID"
'set the default value to the current resolution index
Try
comboCell.Value = resolutions(currentResIndex)
Catch ex As Exception
End Try
comboCell.ValueType = GetType(cmbStruct)
comboCell.DataSource = resolutionList
editCameraTable("Resolution", i) = comboCell
Next
Change the EditMode property:
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
There seems to be a nearly identical question and a very good answer. It involves using the click_event. Here is the link:
How to manually drop down a DataGridViewComboBoxColumn?
In the link:
Private Sub cell_Click(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1.BeginEdit(True)
If DataGridView1.Rows(e.RowIndex).Cells(ddl.Name).Selected = True Then
DirectCast(DataGridView1.EditingControl, DataGridViewComboBoxEditingControl).DroppedDown = True
End If
End Sub

Setting backgroundcolors in DataGridViewCells depending on the value from SQL (VB.Net winforms)

As the title says.
I collect a bunch of values from a SQL Compact DB, and puts them into a DataGridView. One cell ("Status" in this case) contains a varchar field. I want to set the background color of this field depending on its value.
For example, if this value is == "5" or "V", i want it to be red, if it's "4" or "B" i want it green or something like that.
I've tried with a loop that checks every value in this cell and sets the background color, but when i click in the DataGridView headers to change the order of the values the colors disapears.
And... It does not feel right to achieve this result by looping the values afterwards since there's quite much data.
I collect the values with something like this:
Dim Source as Bindingsource = GetBinding()
Form1.ClientsDataGrid.Columns("CustomerNr").DataPropertyName = "CustNR"
Form1.ClientsDataGrid.Columns("CustomerName").DataPropertyName = "Name"
Form1.ClientsDataGrid.Columns("Status").DataPropertyName = "Status"
Form1.ClientsDataGrid.DataSource = Source
'Just in case, this is how i set the colors now
For Each TblRow As DataGridViewRow In Form1.ClientsDataGrid.Rows
If TblRow.Cells(3).Value.ToString = "5" Or TblRow.Cells(3).Value.ToString = "V" Then
TblRow.Cells(3).Style.BackColor = Color.Red
ElseIf (TblRow.Cells(3).Value.ToString = "4" Or TblRow.Cells(3).Value.ToString = "B" Then
TblRow.Cells(3).Style.BackColor = Color.Green
End If
Next
and the GetBinding() looks something like this:
'blah blah make connections
Dim Com As New SqlCeCommand("SELECT Customernumber AS CustNR, Customername AS Name, Customerstatus AS Status FROM Mytable", Con)
Dim dataAdapter As SqlCeDataAdapter
dataAdapter = New SqlCeDataAdapter(Com)
Dim dataSet As New DataSet
dataAdapter.Fill(dataSet, "Mytable")
Dim bind As BindingSource
bind = New BindingSource(dataSet, "Mytable")
Con.Close()
Com = Nothing
Return bind
Is there a way to set these rules directly to the DataGridView? I can of course do the looping every time i sort the list, but it does'nt feel right?
Do that in rowprepaint event .. apply to the row ..
Private Sub ClientsDataGrid_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles ClientsDataGrid.RowPrePaint
Dim tblRow as DataGridViewRow = ClientsDataGrid.Rows(e.RowIndex)
If (TblRow.Cells(3).Value.ToString = "5" Or TblRow.Cells(3).Value.ToString = "V" Then
tblRow.DefaultCellStyle.BackColor = Color.Red
ElseIf (TblRow.Cells(3).Value.ToString = "4" Or TblRow.Cells(3).Value.ToString = "B" Then
tblRow.DefaultCellStyle.BackColor = Color.Green
End If
End Sub