Re-Selecting The Currently Selected Row After A DataGrid Sort.
This is a follow-up to this previous post about sorting a DataGrid. It turns out that the person who originally asked the question about knowing when the sort would occur, wanted to know this for the purpose of re-selecting the currently selected row after the sort. Well, as it turns out this is entirely possible, but it requires some deeper digging into the data-binding aspects of WinForms (which I love btw). ;)
So here's what you'll need to do. First things first, you're going to need to watch for the sort to happen as was detailed in the previous post. Upon detecting the sort about to happen you must record a key value for the currently selected row in a state field (probably in your Form). Now, here's where the deeper knowledge of data-binding comes into play. You must also attach an event listener to the ItemChanged event of the CurrencyManager for the DataTable/DataView you've bound your DataGrid to. The ItemChanged event will fire after the sort takes place at which point you use the DataView's (DataTable::DefaultView if you're bound directly to a DataTable) Find method to locate the row in the view by the key value that you stored in your state field in the sort detection event logic. Once you've found the new position of the row in the DataView after the sort, you set the CurrencyManager's Position property to that index.
Ok, this might sound complex, but lemme show you just how easy it is with some code. For the purposes of keeping this code concise, assume this code is in a Form subclass which contains a DataGrid in a member variable called "myDataGrid". This DataGrid is then bound to a DataTable, stored in a member variable called "myDataTable" which has a key column named "key" of type int:
<codeSnippet language="C#"> public class MyForm : Form { private int currentlySelectedRowId = -1;
public MyForm() { this.InitializeComponent();
// Get the CurrencyManager for the bound DataTable CurrencyManager dataTableCurrencyManager = (CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];
// Hook up to the ItemChanged event dataTableCurrencyManager.ItemChanged += new ItemChangeEventHandler(this.myDataTableCurrencyManager_ItemChanged); }
private void myDataGrid_MouseUp(object sender, MouseEventArgs args) { DataGrid.HitTestInfo hitTestInfo = this.myDataGrid.HitTest(args.X, args.Y);
if(hitTestInfo.Type == DataGrid.HitTestType.ColumnHeader) { // Get the CurrencyManager for the bound DataTable CurrencyManager dataTableCurrencyManager = (CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];
// Get the current DataRowView DataRowView currentRowView = (DataRowView)dataTableCurrencyManager.Current;
// Remember the currently selected row ID this.currentlySelectedRowId = (int)currentRowView["key"]; } }
private void myDataTableCurrencyManager_ItemChanged(object sender, ItemChangeEventArgs args) { // Only execute this logic if we're in the state of sorting if(this.currentlySelectedRowId != -1) { // Find the new position of the row now that it's been sorted int newPosition = this.myDataTable.DefaultView.Find(this.currentSelectedRowId);
// Get the CurrencyManager for the bound DataTable CurrencyManager dataTableCurrencyManager = (CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];
// Change the position of the currency manager dataTableCurrencyManager.Position = newPosition;
// Reset sorting state this.currentSelectedRowId = -1; } } } </codeSnippet> [Drew's Blog]
11:24:11 AM
|