The Wagner Blog
Development Notes, News and Trivia









Subscribe to "The Wagner Blog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Thursday, January 23, 2003
 

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    

Virginia Heinlein died Saturday: "Virginia Heinlein, wife of the late Robert A. Heinlein, the first grandmaster of science fiction, died in her sleep early today. Robert's beloved Ginny was his inspiration and partner. Robert had suffered a variety of ailments during his life and her care helped keep him alive during the most productive and creative stage of his science fiction writing career." [From the Desktop of Dane Carlson]
11:22:07 AM    

The Lost Cosmonauts: "One day in early 1961, weeks before Yuri Gagarin's epic space flight, instead of the usual beeping tones which they had become accustomed to hear, they were startled by a sound which signaled a new chapter in the history of mankind: there, in the listening center of "Torre Bert", these two young students heard, clearly and unequivocally, the beat of a failing heart and the last gasping breaths of a dying cosmonaut." [From the Desktop of Dane Carlson]
11:19:57 AM    


Click here to visit the Radio UserLand website. © Copyright 2004 Thomas Wagner.
Last update: 5/2/2004; 4:46:01 PM.
This theme is based on the SoundWaves (blue) Manila theme.
January 2003
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Dec   Mar