0

How a DataGridView keeps the position of a selected row after event population in VB.NET.

I want without using bindingsource whether it can be applied please guide.

For database information, the table does not have an ID but has a primarykey of 4 columns, namely:CODEPRODUCT,BARCODE,COLORCODE,SIZE

Thanks

 Private WithEvents dt As New DataTable
  Dim dataAdapter As OleDbDataAdapter
  Dim GetConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\demo.accdb;Persist Security Info=False;"
    Private Sub population()
        Try
            dt = New DataTable
            Dim query As String = "SELECT * from stocks"
            Using con As OleDbConnection = New OleDbConnection(GetConnectionString)
                Using cmd As OleDbCommand = New OleDbCommand(query, con)
                    Using da As New OleDbDataAdapter(cmd)
                        'Dim dt As DataTable = New DataTable()
                        da.Fill(dt)
                        da.Dispose()
                        DataGridView1.DataSource = dt
                        Me.DataGridView1.Refresh()
                    End Using
                End Using
            End Using
        Catch ex As Exception
        End Try
    End Sub
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        Me.DataGridView1.MultiSelect = False
        population()
    End Sub
  Private Sub Btnrefresh_Click(sender As Object, e As EventArgs) Handles Btnrefresh.Click
        population()
    End Sub

result in datagridview 19082023

result in datagridview 19082023

roy
  • 693
  • 2
  • 11
  • 1
    If you reload the same data (not clear why), you can simply store the `CurrentCell` index, otherwise you have to store a value that represents the `ID` of the actual data, then find it -- You have more than one thing to fix in that code – Jimi Aug 19 '23 at 04:08
  • @Jimi , thanks your reply, this is only a sample so if I update then I have to call the "population" method and in the actual data it does not have an "ID" but for the 4 columns it is primarykey (CODEPRODUCT,BARCODE,COLORCODE,SIZE). please guide me – roy Aug 19 '23 at 05:02
  • Are you actually talking about a selected row or the current row, because they are two different things? A selected row is highlighted in the grid and is included in the `SelectedRows` collection. The current row is the row containing the cell that contains the caret and is accessed via the `CurrentRow` property. The current row may be selected or it may not. If you're talking about selection then how or even if the grid is bound is irrelevant, because it's specifically a UI matter. If you're talking about the current row then you absolutely should be using a `BindingSource`. – jmcilhinney Aug 19 '23 at 05:28
  • As is so often the case, you're basically aski9ong for code without thinking about the logic that code has to implement. That logic should be pretty obvious. Get the PK of the ow in question first, then reload the data, then find the row containing the stored PK value and act as necessary. This is basic logic that we all use all the time and not specific to programming, so your level of programing experience doesn't matter. Only when you actually understand the logic should you even consider writing code. We should be able to see your best effort to implement that logic. – jmcilhinney Aug 19 '23 at 05:32
  • @jmcilhinney , Thank you for your reply, right what you mean `SelectedRows` and `CurrentRow` but I don't use `BindingSource` – roy Aug 20 '23 at 09:09

1 Answers1

0

We thank them both for providing the best solution for us

In accordance with the solution from @RezaAghaei Here's a link!

and In accordance with the solution from @R.Akhlaghi Here's a link!

I tried the code combination from them finally got a solution .

    Private pk1 As String
    Private pk2 As String
    Private pk3 As String
    Private pk4 As String
    Private rowIndex, scrollIndex As Integer
    Private IsSelectedRow As Boolean
  Private Sub Btnrefresh_Click(sender As Object, e As EventArgs) Handles Btnrefresh.Click
        Dim currentIndex As Integer = DataGridView1.CurrentRow.Index
        pk1 = DataGridView1.SelectedRows(0).Cells(0).Value
                pk2 = DataGridView1.SelectedRows(0).Cells(1).Value
                pk3 = DataGridView1.SelectedRows(0).Cells(2).Value
                pk4 = DataGridView1.SelectedRows(0).Cells(3).Value
                scrollIndex = DataGridView1.FirstDisplayedScrollingRowIndex
        IsSelectedRow = True
        population()
        Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(currentIndex).Cells(0)
    End Sub
    Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
        If IsSelectedRow Then
            For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value.ToString().Equals(pk1.ToString()) AndAlso row.Cells(1).Value.ToString().Equals(pk2.ToString()) AndAlso row.Cells(2).Value.ToString().Equals(pk3.ToString()) AndAlso row.Cells(3).Value.ToString().Equals(pk4.ToString()) Then
                    rowIndex = row.Index
                    Exit For
                End If
            Next row
            DataGridView1.Rows(rowIndex).Selected = True
        End If
    End Sub
roy
  • 693
  • 2
  • 11