I am using ADO.Net datadridview. I am updating the data in a paginated row in the datagridview. With the following code, I stay on the same page, but I cannot stay on the updated row. How can I be both on the same page and on the row I'm updated on?
this.dataGridView1.DataSource = GetCurrentRecords(this.CurrentPageIndex, conn);

Amit MohantyRank #112 · 20K reputationPosted Feb 21, 2024, 12:44 AM EST
Try this:
int currentPageIndex = this.CurrentPageIndex; int selectedRowIndex = dataGridView1.SelectedCells[0].RowIndex; this.dataGridView1.DataSource = GetCurrentRecords(currentPageIndex, conn); if (dataGridView1.Rows.Count > selectedRowIndex) { dataGridView1.Rows[selectedRowIndex].Selected = true; dataGridView1.CurrentCell = dataGridView1.Rows[selectedRowIndex].Cells[0]; }Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 21, 2024, 1:21 AM EST
Thanks Amit. It worked for me.
Abhishek YadavRank #217 · 11.3K reputationPosted Feb 21, 2024, 12:16 AM EST
After updating the data in a paginated row in the datagridview, you can try to set the CurrentCell property of the datagridview to the updated row to stay on the same page and on the row you updated. Here is an example of how you can achieve this:
// Get the current records on the page this.dataGridView1.DataSource = GetCurrentRecords(this.CurrentPageIndex, conn); // Find the index of the updated row in the datagridview int rowIndex = FindUpdatedRowIndex(); // Implement this method to find the index of the updated row // Set the current cell to the updated row if (rowIndex >= 0) { this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rowIndex].Cells[0]; // Set the current cell to the first column of the updated row }By setting the CurrentCell property of the datagridview to the updated row, the datagridview will stay on the same page and scroll to the updated row. You can customize the above code according to the structure of your datagridview and how you handle the updated row.