Infragistic 的UltraGrid控件不提供单击cell的事件,但有时候我们又需要这个事件,所以就只好自己写了:
- 使用UltraGrid的MouseClick事件,这样可以取得单击的Cell,然后就可以对Cell做需要的操作
- MouseClick代码
1
private void ultraGrid1_MouseClick(object sender, MouseEventArgs e)
2{
3UIElement mainElement = ((IUltraControlElement)UltraGrid1).MainUIElement;
4UltraGridCell cell= null;
5//you can define what you want , such as UltraGridCell, UltraGridRow, UltraGridColumn or ColumnHeader
6//UltraGridRow row=null;
7UIElement element = mainElement.ElementFromPoint(new Point(e.X, e.Y));
8while (element != null && cell== null)//replace cell with row,column or column header
9{
10cell= PrepareCell(element);
11//row = PrepareRow(element);
12if (cell== null) //replace cell with row,column or column header
13element = element.Parent;
14}
15if(cell == null)
16return;
17//here you get the cell you need, then you can do any thing you want
18}
- PrepareCell代码
1
//use the same way to get UltraGridRow or UltraGridColumn, etc.
2UltraGridCell PrepareCell(UIElement element)
3{
4//cast the UIElement to the specified element you want
5CellUIElement cellElement = element as CellUIElement ;
6if (cellElement == null)
7return null;
8//get the context from the element
9UltraGridCell cell= cellElement .GetContext(typeof(UltraGridCell)) as UltraGridCell;
10return cell;
11}
- 当需要给UltraGridCell 加ToolTip时也可以用相同的办法实现,不过是要实现MouseMove and MouseLeave事件