Jerry Nixon @Work: Use VSTO 3 in Excel 2007 to trap Selection Changed in a ListObject

Jerry Nixon on Windows

Wednesday, November 19, 2008

Use VSTO 3 in Excel 2007 to trap Selection Changed in a ListObject

In my previous post I created a simple ListObject on a WorkSheet. But what if you wanted to know when the user selected a certain row? Could you use the events built into the ListObject to accomplish this? Yes. But those are difficult to use, I think. Instead, let's use the CurrencyManager which is nestled in the DataSource of the ListObject. Just watch:
 

// handle selection

System.Windows.Forms.CurrencyManager _CurrencyManager;
_CurrencyManager = _ListObject.BindingContext[_Source]
as System.Windows.Forms.CurrencyManager;

_CurrencyManager.CurrentChanged += delegate(object xsender, EventArgs xe)
{
// the selected record is in:
// (_CurrencyManager.Current as Database.Employee).EmployeeID;
};