I didn't find any good solution for my issue. A bit of explanations. I have DataGridView with a few Columns. In the first Column, the cell shows the path to some file. Starting from the third Column, I have couple of ComboBoxCells.
In the DropDown of the ComboBoxes, I wish to make auto-select equal (case insensitive) values to the part of the File path in the first cell. All this happens on Form.Load
.
Later, users make their own choices.
Dim CmbCell As DataGridViewComboBoxCell = CType(DgvRow.Cells(2), DataGridViewComboBoxCell)
Dim ResultString As String
If HasDropDownThisValue(DgvRow.Cells(0).Value.ToString, CmbCell, ResultString) Then
CmbCell.Value = CmbCell.Items(CmbCell.Items.IndexOf(ResultString))
End If
This is the method used in the If
statement:
Private Function HasDropDownThisValue(ByVal GivenValue As String, ByRef comboCeel As DataGridViewComboBoxCell, ByRef ReturnValue As String) As Boolean
ReturnValue = ""
Dim boolret As Boolean = False
Dim comparestring As String
Dim StringArr() As String = Split(GivenValue, CStr(Path.DirectorySeparatorChar), -1, VisualBasic.CompareMethod.Text)
comparestring = StringArr(0)
For i As Integer = 1 To UBound(StringArr)
If comboCeel.Items.Cast(Of String).Contains(comparestring, StringComparer.OrdinalIgnoreCase) Then
ReturnValue = comparestring
boolret = True
Exit For
Else
comparestring = String.Format("{0}{1}{2}", comparestring, Path.DirectorySeparatorChar, StringArr(i))
End If
Next
Return boolret
End Function
The line:
CmbCell.Value = CmbCell.Items(CmbCell.Items.IndexOf(ResultString))
throws an exception when the case doesn't match. How to avoid that?
I probably can do something like: CmbCell.Value = ResultString
, but I prefer to pre-define values in Items collection.
Maybe something like this is possible (used in a function):
comboCeel.Items.Cast(Of String).Contains(comparestring, StringComparer.OrdinalIgnoreCase)