Keywords: DevExpress XtraGrid | Data Binding | Event Handling
Abstract: This article provides an in-depth exploration of various techniques for retrieving selected row data in the DevExpress XtraGrid control. By comparing data binding, event handling, and direct API calls, it details how to efficiently extract and display selected row information in different scenarios. Focusing on the best answer from Stack Overflow and incorporating supplementary approaches, the article offers complete code examples and implementation logic to help developers choose the most suitable method for their needs.
Introduction
In Windows Forms application development, DevExpress XtraGrid is a powerful data grid control widely used for data presentation and interaction. Unlike the standard DataGridView control, XtraGrid offers richer APIs and event models, making the methods for retrieving selected row data more diverse. This article systematically introduces several mainstream implementation approaches and analyzes their respective application scenarios.
Data Binding Approach
Data binding is one of the most elegant ways to achieve synchronized data display. By using Binding objects, properties of controls like text boxes can be directly bound to data sources, enabling automatic updates. The core advantage of this method lies in reducing manual code and improving maintainability.
textBox1.DataBindings.Add(new Binding("Text", yourBindingSource, "TableName.ColumnName", true, DataSourceUpdateMode.OnPropertyChanged));In the above code, yourBindingSource is the data source connected to XtraGrid, and TableName.ColumnName specifies the data field to bind. Setting DataSourceUpdateMode.OnPropertyChanged ensures immediate UI updates upon data changes. This method is particularly suitable for scenarios requiring continuous data synchronization but demands a good understanding of data binding mechanisms.
Event-Driven Methods
XtraGrid provides rich events to handle user interactions, with the FocusedRowChanged event being a common way to retrieve focused row data. When users select different rows, this event is triggered, allowing developers to update related controls.
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) {
textBox1.Text = gridView1.GetFocusedRowCellValue("Name").ToString();
}Besides GetFocusedRowCellValue, several other equivalent APIs can be used:
gridView1.GetDataRow(e.FocusedRowHandle)["Name"].ToString(): Retrieves a DataRow object via row handle.gridView1.GetFocusedDataRow()["Name"].ToString(): Directly obtains the DataRow of the focused row.(gridView1.GetFocusedRow() as DataRowView).Row["Name"].ToString(): Retrieves data through type casting.
The choice among these methods depends on the specific data source type and developer preferences. GetFocusedRowCellValue is the most straightforward and suitable for most scenarios.
Handling Multiple Row Selections
When dealing with multiple row selections, the GetSelectedRows method can be used to obtain an array of indices for all selected rows. This approach allows developers to iterate through multiple selected rows for batch operations.
int[] selRows = ((GridView)gridControl1.MainView).GetSelectedRows();
DataRowView selRow = (DataRowView)(((GridView)gridControl1.MainView).GetRow(selRows[0]));
txtName.Text = selRow["name"].ToString();Using the selRows array, data from each selected row can be accessed. For instance, if aggregating information from multiple selected rows is needed, iterating through this array and summing relevant field values is effective. This method is particularly useful in scenarios requiring handling of user multi-selection operations.
Cell Click Events
Beyond row-level events, XtraGrid supports cell-level interaction handling. The RowCellClick event enables developers to execute custom logic when users click specific cells.
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) {
TBGRNo.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "GRNo").ToString();
TBSName.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "SName").ToString();
TBFName.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FName").ToString();
}This method is similar to the FocusedRowChanged event but offers finer-grained control. For example, different actions can be performed based on the clicked column index. Note that cell clicks might not change the focused row, so ensuring the correct row handle is used is crucial.
Performance and Best Practices
When selecting a specific implementation method, consider performance impacts and code maintainability. The data binding approach reduces event-handling code but may increase initial configuration complexity. Event-driven methods offer quick responsiveness, ideal for scenarios requiring real-time feedback. For large datasets, avoid complex computations or frequent database queries within event handlers.
A good practice is to unify data access logic, such as creating a helper method to retrieve selected row data and calling it across different events. This enhances code reusability and testability.
Conclusion
DevExpress XtraGrid offers multiple flexible ways to retrieve selected row data, allowing developers to choose the most suitable approach based on specific needs. Data binding is ideal for automatic synchronization scenarios, event-driven methods provide more direct control, and multi-row selection handling expands interaction possibilities. By appropriately combining these techniques, efficient and user-friendly data interfaces can be built.