TryGetClickedID
(
Microsoft.Office.Interop.Excel.Range oClickedRange,
Microsoft.Office.Interop.Excel.ListObject oTable,
String sIDColumnName,
out Int32 iClickedID
)
{
Debug.Assert(oClickedRange != null);
AssertValid();
iClickedID = Int32.MinValue;
// Note: Don't use oClickedRange to get the clicked cell. This works
// if only one cell is selected, but if the selection is larger than
// one cell, oClickedRange is the entire selection.
//
// Instead, get the clicked cell via the Window.RangeFromPoint()
// method.
Point oClickedPoint = Control.MousePosition;
Microsoft.Office.Interop.Excel.Range oClickedCell =
(Microsoft.Office.Interop.Excel.Range)
oClickedRange.Application.ActiveWindow.RangeFromPoint(
oClickedPoint.X, oClickedPoint.Y);
if (oClickedCell == null)
{
return (false);
}
// Attempt to get the table's optional ID column.
Microsoft.Office.Interop.Excel.ListColumn oIDColumn;
if (!ExcelTableUtil.TryGetTableColumn(oTable, sIDColumnName,
out oIDColumn) )
{
return (false);
}
// Attempt to get an ID from the clicked row.
Debug.Assert(oClickedRange.Parent is
Microsoft.Office.Interop.Excel.Worksheet);
Microsoft.Office.Interop.Excel.Worksheet oWorksheet =
(Microsoft.Office.Interop.Excel.Worksheet)oClickedRange.Parent;
Microsoft.Office.Interop.Excel.Range oIDRange =
(Microsoft.Office.Interop.Excel.Range)oWorksheet.Cells[
oClickedCell.Row, oIDColumn.Range.Column];
String sID;
if ( !ExcelUtil.TryGetNonEmptyStringFromCell(
ExcelUtil.GetRangeValues(oIDRange), 1, 1, out sID)
||
!Int32.TryParse(sID, out iClickedID) )
{
return (false);
}
return (true);
}