How to change row position of DataGridView virtual mode?

How to change row position of DataGridView virtual mode?

I am using Windows Forms .

+1


source to share


5 answers


Marcus's answer is correct, but you may also need to set the current cell property of the DataGridView ...

dgv.CurrentCell = dgv.Rows[0].Cells[0];

      

I believe this will scroll the grid. Also, to be completely safe, you can add this before another line of code ...



dgv.CurrentCell = null;

      

This will ensure that if the row you want is already the active row, but just scrolls out of view, it will scroll back into view.

+2


source


You need to clear the old position and set the new one.

The data of the GridView1.SelectedRows collection has the currently selected rows. Depending on the MultiSelect property of the grid, you may need to loop through all the rows in the SelectedRows and mark them as unselected. If you are the only selection mode, simply set the new row as selected to clear the old selection.



To select a specific row (in this case one of the indices 0), you simply add the row dataGridView1.Rows [0] .Selected = true;

+3


source


Private Sub GridSaleItem_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridSaleItem.SelectionChanged
    Dim rowcount As Integer
    rowcount = GridSaleItem.Rows.Count
    For i As Integer = 1 To rowcount
        If i = 1 Then
            '
        Else
            If i = rowcount Then
                Me.GridSaleItem.CurrentCell = Me.GridSaleItem.Rows(i - 1).Cells(0)
                Me.GridSaleItem.Rows(i - 1).Selected = True
            End If
        End If
    Next

End Sub

      

0


source


Else
        If i = rowcount Then
            Me.GridSaleItem.CurrentCell = Me.GridSaleItem.Rows(i - 1).Cells(0)
            Me.GridSaleItem.Rows(i - 1).Selected = True
        End If
    End If
Next

      

0


source


You seem to need not only setting the selected row, but also the displayed row. You can access the latter using a property FirstDisplayedScrollingRowIndex

in your DataGridView. One of the useful settings:

int lastShown = FirstDisplayedScrollingRowIndex + DisplayedRowCount(false) - 2;

if (lastShown < yourIndex)
  FirstDisplayedScrollingRowIndex += yourIndex - lastShown;
else if (FirstDisplayedScrollingRowIndex > yourIndex)
  FirstDisplayedScrollingRowIndex = yourIndex;

      

make sure your newly selected row doesn't disappear from the screen when scrolling up / down programmatically.

0


source







All Articles