0

Good Evening.

I have a program in VB.Net that refreshes datagridview after you edit some data my problem here is im populating over 1000 records.

Let say Im editing row 999 and after i click update the data will refresh causing the datagridview to return at the top (The blue highlighter)

My goal here is how can I maintain it to its current position after update?

My solution here is highlight the data where Textbox1 = value

Is this possible like this?

'SAMPLE CODE
Datagridview1.Column(0).value.BlueHighLighter = Textbox1.text

Pls see my code on how i refresh DGV

  Dim con11 As MySqlConnection = New MySqlConnection("server=192.168.2.87;userid=root;password=admin1950;database=inventory")
        Dim sql1 As MySqlCommand = New MySqlCommand("select PONo,ItemCode,Description,QtyPack,PackUoM,QtyStan,StanUoM,UnitPrice,Total,Remarks,ExpiryDate from Receiving where RINo = '" & Add_Receiving_Items.TextBox1.Text & "';", con1)
        Dim ds1 As DataSet = New DataSet
        Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
        con1.Open()
        adapter1.SelectCommand = sql1
        adapter1.Fill(ds1, "MyTable")
        Add_Receiving_Items.DataGridView1.DataSource = ds1.Tables(0)
        con1.close()
        With Add_Receiving_Items.DataGridView1()
            .RowHeadersVisible = False
            .Columns(0).HeaderCell.Value = "PO No"
            .Columns(1).HeaderCell.Value = "Item Code"
            .Columns(2).HeaderCell.Value = "Description"
            .Columns(3).HeaderCell.Value = "Quantity/Pack"
            .Columns(4).HeaderCell.Value = "Packaging UoM"
            .Columns(5).HeaderCell.Value = "Quantity/Pc"
            .Columns(6).HeaderCell.Value = "Standard UoM"
            .Columns(7).HeaderCell.Value = "Unit Price"
            .Columns(8).HeaderCell.Value = "Total"
            .Columns(9).HeaderCell.Value = "Remarks"
            .Columns(10).HeaderCell.Value = "Expiry Date"
        End With

        Add_Receiving_Items.DataGridView1.Columns.Item(0).Width = 80
        Add_Receiving_Items.DataGridView1.Columns.Item(1).Width = 80
        Add_Receiving_Items.DataGridView1.Columns.Item(2).Width = 120
        Add_Receiving_Items.DataGridView1.Columns.Item(3).Width = 86
        Add_Receiving_Items.DataGridView1.Columns.Item(4).Width = 68
        Add_Receiving_Items.DataGridView1.Columns.Item(5).Width = 75
        Add_Receiving_Items.DataGridView1.Columns.Item(6).Width = 68
        Add_Receiving_Items.DataGridView1.Columns.Item(7).Width = 70
        Add_Receiving_Items.DataGridView1.Columns.Item(8).Width = 80
        Add_Receiving_Items.DataGridView1.Columns.Item(9).Width = 105
        Add_Receiving_Items.DataGridView1.Columns.Item(10).Width = 63
        Add_Receiving_Items.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        Add_Receiving_Items.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        With Add_Receiving_Items.DataGridView1
            .RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
            .AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender
        End With

TYSM for future help

Nyx Assasin
  • 141
  • 1
  • 11

1 Answers1

0

Use DataGridView.CurrentRow property. But please be aware that CurrentRow is ReadOnly, you must use CurrentCell

Before refreshing your data store Dim oldIndex = DataGridView.CurrentRow.Index and after the refresh set DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)

EDIT:

How to test the Code

Create a form with a Button1 and a DataGridView1 with two columns and paste the following code:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 1 To 5
            DataGridView1.Rows.Add("foo" & i, "bar" & i)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim currentIndex = DataGridView1.CurrentRow.Index
        currentIndex += 1
        If currentIndex >= DataGridView1.Rows.Count Then currentIndex = 0

        DataGridView1.CurrentCell = DataGridView1.Rows(currentIndex).Cells(0)
    End Sub
End Class

EDIT:

Dim oldIndex = DataGridView.CurrentRow.Index
'Put your code here on how you refresh your data
DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)
Luke
  • 751
  • 2
  • 7
  • 32
theBugger
  • 441
  • 3
  • 14
  • I think the reason why someone down voted you without commenting there reason is because when i tried your code the output is when i click the button the blue selector always go down by one which is not on what i expected. My target here is go back to the current cell after refreshing the row. lets say I edit "bar3" of ''foo3" and after i edit it i want to return it back to foo3 again – Nyx Assasin Sep 01 '16 at 02:27
  • For now I will up vote your answer so it will return to neutral. Hoping to see the in your post very soon TYSM for future help – Nyx Assasin Sep 01 '16 at 02:28
  • Mine was an example to prove that you can use `.CurrentCell`. I gave you what you needed, you can't pretend me to write the code for you... – theBugger Sep 01 '16 at 05:53