I’m trying to format the length of a DataGridViewComboBoxColumn based on the length of the longest string in the comboBox. Here is the code I currently have, but it only formats the DataGridViewComboBoxColumn based on the users previous selection in the comboBox.
Is there a way to have the DataGridViewComboBoxColumn at the length of the longest string in the comboBox?
Here is my code:
Private Sub comboTest_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles comboTest.SelectionChangeCommitted
Dim senderComboBox As ComboBox = CType(sender, ComboBox)
'Change the length of the text box depending on what the user has
'selected and committed using the SelectionLength property.
If (comboTest.SelectionLength > 0) Then
comboTest.Width = comboTest.SelectionLength * CType(Me.comboTest.Font.SizeInPoints, Integer)
comboTest.SelectedValue = comboTest.SelectedText
End If
End Sub
It seems like you just need to calculate the longest string in the
ComboBox. Assuming that it’s just a collection ofStringvalues being displayed you can do the following.