VB - Adding to List(of String) from datagridview column/cell value
-
(SOLVED) I have played with this, but I am missing the obvious. Can I borrow your brains again? (I dislike working alone.) I make a selection in the datagridview. This selection is now processed in the make list handle. I bold the first column's text for identity on the datagridview. I want to keep the value from the cell in a list. I get out of range and null exceptions. At this point, I do not have the cleanest code as I am working on operational aspects of the grid. So, below the instruction to bold the selected, I am counting the bolded (which works fine) for a count value on the form to be displayed, but I cannot add the "job number" to my list. And before you ask, yes the job number is a string. I checked that first! Original declaration is ... dim selectedJobNo as list(of string)
Private Sub MakeListToolStrip_Click_1(sender As Object, e As EventArgs) Handles MakeListToolStrip.Click
Dim f As Font = New Font(JobsDataGridView.Font, FontStyle.Bold) For selectrow = 0 To JobsDataGridView.SelectedRows.Count - 1 JobsDataGridView.SelectedRows(selectrow).Cells(0).Style.Font = f Next Dim i As Integer = 0 Dim count As Integer = 0 selectedJobNo = New List(Of String) For i = 0 To JobsDataGridView.Rows.Count - 1 If JobsDataGridView.Rows(i).Cells(0).InheritedStyle.Font.Bold Then count = count + 1 selectedJobNo.Add(JobsDataGridView.SelectedRows(i).Cells(0).Value.ToString) MsgBox(selectedJobNo.Count) End If Next JobSelectedToolStrip.Text = count 'remove highlight selection and deselect any row highlight Me.JobsDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect End Sub
Solution: I am leaving this post in case someone tries a similar method. However, I switched to a collection! I don't know why I didn't think of this. I am so very accustomed to just using a list. Here is what I did ... 1. When item selected in the DGV. I bolded the index/reference cell only. I add to the collection. 2. When removing an item from the custom selected list, I un-bold the index/reference cell. I remove from the collection. 3. When they switch between sorts, I run through the collection to bold the index/reference cells (because the DGV gets a new datasource selected based on the predefined sorts open/closed/all). 4. On request to print,