This is my new post but in continuation of Thread 78734244 of this forum.
I was wondering How Could i get all the Cell Addresses/Reference in Datagridview. The above thread was for
any single input of Cell Value to get its respective Cell Address in Datagridview.
Now I would like to have all Cell Addresses/References of DataGridView
ie for the below grid
6 9 7
8 1 5
2 4 3
and get list like below
6 is in cell (0,0)
9 is in cell (0,1)
7 is in cell (0,2)
8 is in cell (1,0)
1 is in Cell (1,1)
5 is in Cell (1,2)
2 is in Cell (2,0)
4 is in cell (2,1)
6 is in cell (2,2)
So in order to get the multiple cell reference
First i’ve Created numbers as List
Dim numbers As String() = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
and by searching the above nos i get the multiple cell.Address Reference of DataGridview
I don’t know whether the below Function is Correct for the desired result.
After Getting the below list of all the Cell Address I would like to use the same cell Address for more datagridviews
What would you suggest to store the same in List or Array ?
Your help will be appreciated
Thanks SsD
Private Sub MultipleCells_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cellsList As New ArrayList
For i As Integer = 1 To UBound(numbers)
Dim cell = mulitpleGetCellAddress(DataGridView1, numbers(i))
If cell IsNot Nothing Then
'MessageBox.Show($"Data exists in cell ({cell.RowIndex}, {cell.ColumnIndex})")
cellsList.Add($"", i & ({cell.RowIndex}, {cell.ColumnIndex})) '& ({cell.RowIndex}, {cell.ColumnIndex}))
Else
'MessageBox.Show("Data does not exist!")
End If
'i = i + 1
Next
End Sub
Private Function mulitpleGetCellAddress(dgv As DataGridView, searchString As String) As DataGridViewCell
For Each row In dgv.Rows.OfType(Of DataGridViewRow)
For Each cell In row.Cells.OfType(Of DataGridViewCell)
If cell.Value.ToString() = searchString Then
Return cell
End If
Next
Next
Return Nothing
End Function
made a Blunder in the line
```cellsList.Add($"", i & ({cell.RowIndex}, {cell.ColumnIndex})) '& ({cell.RowIndex}, {cell.ColumnIndex})) ```
3
Since we’re iterating over a UI element, we would want to limit the number of iterations. Let’s use an iterator function returning all the matches, with each cell only being checked once – well the Any
will stop checking once a match is found and even though both your idea and this one are O(n2), this one is so to a lesser degree.
Private Iterator Function mulitpleGetCellAddress(dgv As DataGridView, searchStrings As IEnumerable(Of String)) As IEnumerable(Of DataGridViewCell)
For Each row In dgv.Rows.OfType(Of DataGridViewRow)
For Each cell In row.Cells.OfType(Of DataGridViewCell)
If searchStrings.Any(Function(s) cell.Value.ToString() = s) Then Yield cell
Next
Next
End Function
Now you will have a list of cells which match any of the sought items
Dim cells = mulitpleGetCellAddress(DataGridView1, numbers.Select(Function(n) $"{n}"))
If cells IsNot Nothing Then
MessageBox.Show($"Data exists in cells {String.Join("; ", cells.Select(Function(c) $"({c.RowIndex}, {c.ColumnIndex})"))}")
Else
MessageBox.Show("Data does not exist!")
End If
5