0
votes

I've read around and googled but didn't find the little thing which would help me. I think I've got some simple logical error. Ok, so here is what I have: A DataGridView bound to a Datatable populated with filenames (and other info, mainly audio format). Well, now I've written a sub (VS2013 and using .Net 4.0) for printing which works fine, I tried to add an option to print only selected rows from the DataGridView and thought ok, then I give the printing routine the whole rows of the DataGridView or only the selected rows based on that option. This is what I tried: On the form Level I defined the rowCollection as: Dim rowCollection As DataGridViewRowCollection

The BeginPrint Sub then has the following lines to get the rows:

'*** Get Rows to print
    rowCollection = New DataGridViewRowCollection(FileDataGridView)
    If My.Settings.PrintSelected And (FileDataGridView.SelectedRows.Count > 0) Then
        Dim row As New DataGridViewRow
        For r = FileDataGridView.SelectedRows.Count - 1 To 0 Step -1
            row = FileDataGridView.SelectedRows(r)
            rowCollection.Add(FileDataGridView.Rows(row.Index))
        Next
    Else
        rowCollection = FileDataGridView.Rows
    End If

I thought a rowCollection would be something like an array of the single rows, but apparently this doesn't do the job. If I want to print all rows, this works fine, but for the selected rows, I get an error the no elements could be added to the row collection because the DataGridView is databound. I thought the rowCollection could be population by single rows, an not only link to the whole DataGridView. Where am I wrong? I also tried to get only the .SelectedRows as a collection but then I get type errors as DataGridView.Rows and DataGridView.SelectedRows do not have the same format? I would like to get a list/collection/array of the rows or selected rows, which I then can use for printing. Any help would be appreciated

regards,

Christian

1

1 Answers

0
votes

Surely not a perfect solution but does the job:

    Dim rows As List(Of DataGridViewRow)
    Dim rowarray(DataGridView1.Rows.Count) As DataGridViewRow
    If DataGridView1.SelectedRows.Count > 0 Then
        DataGridView1.SelectedRows.CopyTo(rowarray, 0)
    Else
        DataGridView1.Rows.CopyTo(rowarray, 0)
    End If
    rows = rowarray.ToList
    rows.RemoveAll(Function(a As DataGridViewRow) a Is Nothing)

There is a difference between a SelectedRowCollection and a RowCollection, for what ever reason they didn't used inheritence for those 2, so you need to cheat a little to process both the same way