Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. DevExpress
  4. Repopulating and Reselecting same Row in DevExpress Grid

Repopulating and Reselecting same Row in DevExpress Grid

Date- Jan 19,2024 Updated Feb 2026 3261
Devexpress Devexpress Grid

Overview of DevExpress Grid Control

The DevExpress Grid Control is a powerful data visualization tool that allows developers to present data in a tabular format with extensive features such as sorting, filtering, and editing. One common scenario developers face is the need to update the grid's data source and then reselect the previously selected row. This ensures that users can continue their workflow without interruption, even when the underlying data changes.

For instance, consider a scenario in an inventory management system where an item may be updated in the database. After fetching the latest data, it is essential to maintain the user's focus on the same item they were editing or viewing. This tutorial will guide you through the steps required to achieve this functionality.

Prerequisites

Before diving into the implementation, it is important to have the following prerequisites in place:

  • A working knowledge of C# and the .NET framework.
  • Familiarity with the DevExpress Grid Control.
  • Access to a sample data source that can be used to populate the grid.

Step 1: Save the Key Value of the Selected Row

The first step in reselecting a row after repopulating the grid is to save the key value of the currently selected row. This key value is essential for locating the row in the updated data source. In most cases, this key value corresponds to a unique identifier in your data model.

// Save the key value of the selected row
var selectedRowKey = gridView.GetRowCellValue(gridView.FocusedRowHandle, "YourKeyFieldName");

In this example, replace YourKeyFieldName with the actual name of the field that uniquely identifies each row. It is crucial to ensure that this field is present in both the original and updated data sources.

Step 2: Repopulate the Grid

Once you have saved the key value, the next step is to repopulate the grid with updated data. This typically involves fetching new data from a database or another data source. After obtaining the updated data, you will set it as the data source for the grid control and refresh its view to reflect the changes.

// Repopulate the grid with updated data
yourGridControl.DataSource = GetUpdatedData(); // Fetch updated data from the database
yourGridControl.RefreshDataSource();

Make sure that GetUpdatedData() is a method that retrieves the latest data set that the grid should display. This could involve executing a database query or calling an API endpoint.

Step 3: Re-select the Row Using the Key Value

After the grid has been repopulated, you can now reselect the previously selected row using the key value you saved earlier. This involves locating the new index of the row in the updated data and setting it as the focused row in the grid.

// Locate the new index of the row with the saved key value
int newIndex = gridView.LocateByValue("YourKeyFieldName", selectedRowKey); // Ensure the key exists in the updated data
if (newIndex != DevExpress.XtraGrid.GridControl.InvalidRowHandle) {
    gridView.FocusedRowHandle = newIndex; // Set the focused row
    gridView.SelectRow(newIndex); // Select the row
}

In this code snippet, we check if the newIndex is valid before attempting to set the focused row. This is important to avoid any exceptions that may arise if the key value does not exist in the new data set.

Edge Cases & Gotchas

While the above steps cover the basic process of reselecting a row, there are several edge cases and gotchas to be aware of:

  • Key Value Not Found: If the key value does not exist in the updated data, you should handle this case gracefully. Consider notifying the user or providing a fallback mechanism.
  • Data Structure Changes: If the structure of your data has changed (e.g., columns added or removed), ensure that the key field is still valid and properly mapped.
  • Multiple Selections: If your grid allows for multiple row selections, you will need to adjust your logic to handle an array of selected keys rather than a single key.

Performance & Best Practices

When implementing row re-selection in a DevExpress Grid, consider the following best practices to ensure optimal performance and user experience:

  • Minimize Data Fetching: Only fetch data that has changed since the last update to reduce load times and improve performance.
  • Use Asynchronous Data Loading: Consider using asynchronous methods to load data, which can keep the UI responsive during data fetch operations.
  • Optimize Data Binding: Use efficient data binding techniques to minimize the overhead of data updates.

Conclusion

In this tutorial, we covered the process of reselecting the same row in a DevExpress Grid after repopulating it with new data. By following the outlined steps, you can ensure that users maintain their context when interacting with dynamic data.

  • Key Takeaway 1: Always save the key value of the selected row before repopulating the grid.
  • Key Takeaway 2: Ensure to handle cases where the key value may not exist in the updated data.
  • Key Takeaway 3: Follow best practices for data fetching and binding to enhance performance.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

How to export view as pdf in Asp.Net Core
Jul 05, 2022
Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
How to Import CSV in ASP.NET MVC
Feb 02, 2024
Get random number in asp.net C#
Dec 23, 2023
Buy me a pizza

Comments

On this page

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor