文章参考:http://www.cnblogs.com/surfsky/archive/2008/08/20/1272104.html
下面红颜色标出我在项目中经常用到的内容。
UltraGrid 是Infragistics netadvanage 控件库中提供的一个Windows 网格控件,功能强大,完全可以取代VS 中提供的 GridView控件。但不知为何,国内介绍它的文章很少。这玩意功能是相当强大,但其属性设计原理和普通控件不太一样,属性极为复杂,没有手册几乎无法编码。
参考
提供商首页:http://www.infragistics.com/
术语
persist 持续化(保存)
property 属性
attribute 特性
setting 设置
Band 数据条带
示例图片
■■■■■■■■■■■■■■■■■■■■■■■■■
■■ Actions ■■
■■■■■■■■■■■■■■■■■■■■■■■■■
KeyActionMappings(按键动作映射)
// 使用Enter切换到下一个单元格
using Infragistics.Win.UltraWinGrid;
this.ultraGrid1.KeyActionMappings.Add(
new GridKeyActionMapping(
Keys.Enter, // 按下回车键时
UltraGridAction.NextCell, // 跳转到下一单元格
UltraGridState.IsCheckbox, // 单元格不能为checkbox
UltraGridState.Cell, // 选中单元格时
0, // 不禁止特殊键
0 // 不需要特殊键
)
);
// 按下H键导航到表格的第一行
this.ultraGrid1.KeyActionMappings.Add(
new GridKeyActionMapping(
Keys.H, // 按下H键
UltraGridAction.FirstRowInGrid, // 定位到网格的首行
UltraGridState.InEdit, // 此时网格不处于编辑状态
0, // 无附加状态需求
Infragistics.Win.SpecialKeys.Alt, // 此时Alt键没有按下
0 // 无附加特殊按键需求
)
);
PerformActions(执行预设动作)
switch (e.Tool.Key)
{
case "Undo":
this.ultraGrid1.PerformAction(UltraGridAction.Undo);
break;
case "Redo":
this.ultraGrid1.PerformAction(UltraGridAction.Redo);
break;
case "Copy":
this.ultraGrid1.PerformAction(UltraGridAction.Copy);
break;
case "Paste":
this.ultraGrid1.PerformAction(UltraGridAction.Paste);
break;
case "Cut":
this.ultraGrid1.PerformAction(UltraGridAction.Cut);
break;
case "Delete":
this.ultraGrid1.PerformAction(UltraGridAction.DeleteCells);
break;
case "Select All":
foreach (UltraGridRow row in this.ultraGrid1.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
{
row.Selected = true;
}
break;
}
// 响应键盘事件
private void ultraGrid1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Up:
this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false, false);
e.Handled = true;
this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
break;
case Keys.Down:
this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false, false);
e.Handled = true;
this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
break;
case Keys.Right:
this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false, false);
e.Handled = true;
this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
break;
case Keys.Left:
this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false, false);
e.Handled = true;
this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
break;
}
}
// 设置激活单元格背景色
private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
this.ultraGrid1.DisplayLayout.Override.ActiveCellAppearance.BackColor = Color.Red;
}
// Set ActiveRow and ActiveCell on grid entry
private void ultraGrid1_Enter(object sender, System.EventArgs e)
{
if(this.ultraGrid1.ActiveRow == null)
this.ultraGrid1.ActiveRow = this.ultraGrid1.GetRow(ChildRow.First);
if(this.ultraGrid1.ActiveCell == null)
this.ultraGrid1.ActiveCell = this.ultraGrid1.ActiveRow.Cells[2];
}
■■■■■■■■■■■■■■■■■■■■■■■■■
■■ Grid Events ■■
■■■■■■■■■■■■■■■■■■■■■■■■■
按键受理
private void ultraGrid1_KeyPress(object sender, KeyPressEventArgs e)
{
// For the OrderDate column, change the periods to slashes
switch (this.ultraGrid1.ActiveCell.Column.Key)
{
case "OrderDate":
if(e.KeyChar.ToString() == ".")
{
e.Handled = true;
SendKeys.Send("/");
}
break;
}
}
InitializeLayout
private void UltraGrid1_InitializeLayout(object sender,
Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
e.Layout.Override.AllowAddNew = AllowAddNew.FixedAddRowOnTop;
e.Layout.Override.TemplateAddRowAppearance.BackColor = Color.FromArgb( 245, 250, 255 );
e.Layout.Override.TemplateAddRowAppearance.ForeColor = SystemColors.GrayText;
e.Layout.Override.AddRowAppearance.BackColor = Color.LightYellow;
e.Layout.Override.AddRowAppearance.ForeColor = Color.Blue;
e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.TemplateAddRow;
e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = SystemColors.Control;
e.Layout.Override.TemplateAddRowPrompt = "Click here to add a new record...";
e.Layout.Override.TemplateAddRowPromptAppearance.ForeColor = Color.Maroon;
e.Layout.Override.TemplateAddRowPromptAppearance.FontData.Bold = DefaultableBoolean.True;
e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;
e.Layout.Bands[0].Columns[5].DefaultCellValue = "(DefaultValue)";
e.Layout.ScrollStyle = ScrollStyle.Immediate;
e.Layout.ScrollBounds = ScrollBounds.ScrollToFill;
// Initialize controls
this.LoadAllowAddNewCombo( this.ultraComboEditorAllowAddNew );
this.LoadEnumCombo( this.ultraComboEditorSpecialRowSeparatorBorderStyle, typeof( UIElementBorderStyle ) );
this.LoadColumnsCombo( this.ultraComboEditorSpecialRowPromptField, e.Layout.Bands[0] );
// Initialize the controls to their values.
this.ultraComboEditorAllowAddNew.Value = e.Layout.Override.AllowAddNew;
this.ultraComboEditorSpecialRowSeparatorBorderStyle.Value = e.Layout.Override.BorderStyleSpecialRowSeparator;
this.ultraNumericEditorAddRowSpacingAfter.Value = e.Layout.Override.TemplateAddRowSpacingAfter;
this.ultraNumericEditorAddRowSpacingBefore.Value = e.Layout.Override.TemplateAddRowSpacingBefore;
this.ultraNumericEditorSeparatorHeight.Value = e.Layout.Override.SpecialRowSeparatorHeight;
this.ultraCheckEditorAddNewBox.Checked = ! e.Layout.AddNewBox.Hidden;
this.ultraCheckEditorCardView.Checked = e.Layout.Bands[0].CardView;
this.ultraCheckEditorSeparator.Checked = 0 != ( SpecialRowSeparator.TemplateAddRow & e.Layout.Override.SpecialRowSeparator );
this.ultraTextEditorAddRowPrompt.Value = e.Layout.Override.TemplateAddRowPrompt;
}
■■■■■■■■■■■■■■■■■■■■■■■■
■■ Mouse Events ■■
■■■■■■■■■■■■■■■■■■■■■■■■
确定用户点击的数据条带
private void ultraGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Declare and retrieve a reference to the UIElement
UIElement aUIElement =
this.ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(new Point(e.X, e.Y));
if(aUIElement == null)
return;
UltraGridBand aBand = null;
// Look for a row
UltraGridRow aRow = (UltraGridRow)aUIElement.GetContext(typeof(UltraGridRow));
if(aRow != null)
aBand = this.ultraGrid1.DisplayLayout.Bands[aRow.Band.Index];
if(aBand == null)
{
// Look for a column
UltraGridColumn aColumn =
(UltraGridColumn)aUIElement.GetContext(typeof(UltraGridColumn));
if(aColumn != null)
aBand = this.ultraGrid1.DisplayLayout.Bands[aColumn.Band.Index];
}
// If a band was found display the band index
if(aBand != null)
this.ultraTextEditor1.Text = "Band(" + aBand.Index.ToString() + ")";
else
this.ultraTextEditor1.Text = "* no band associcated with this location";
}
■■■■■■■■■■■■■■■■■■■■■■■■
■■ Row Events ■■
■■■■■■■■■■■■■■■■■■■■■■■■
给每一行展示一个说明
this.ultraGrid1.DisplayLayout.Bands[0].AutoPreviewEnabled = true;
private void datagrid1_InitializedRow(....)
{
e.row.Description = "My description";
}
■■■■■■■■■■■■■■■■■■■■■■■■
■■ Cell Events ■■
■■■■■■■■■■■■■■■■■■■■■■■■
单元格数据修改后事件
ultraGrid1_AfterCellUpdate
当前单元格变动事件
private void datagrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
int inumber = this.datagrid1.currentcell.rownumber;
string smyid = this.datagrid1[inumber, 0].tostring();//获取到第i行第一列的值
}
■■■■■■■■■■■■■■■■■■■■■■■■
■■ Scroll ■■
■■■■■■■■■■■■■■■■■■■■■■■■
滚动到首行
private void ultraButton1_Click(object sender, System.EventArgs e)
{
// If there are no rows this method will not work
if(this.ultraGrid1.Rows.Count <= 0)
return;
this.ultraGrid1.ActiveRow = this.ultraGrid1.Rows[0];
}
上滚一行
// If no ActiveRow this procedure will not work
if(this.ultraGrid1.ActiveRow == null)
return;
// Retrieve reference to current ActiveRow and look for a previous row
UltraGridRow aRow = this.ultraGrid1.ActiveRow;
// Look to see if this row has a Previous Sibling
if(aRow.HasPrevSibling() == true)
{
// Set ActiveRow to this row to make it visible
this.ultraGrid1.ActiveRow = aRow.GetSibling(SiblingRow.Previous);
// Look to see if this row has children
if(this.ultraGrid1.ActiveRow.HasChild())
{
// Look for lowest child in hierarchy
aRow = this.ultraGrid1.ActiveRow;
while(aRow.HasChild() != false)
{
aRow = aRow.GetChild(ChildRow.Last);
}
this.ultraGrid1.ActiveRow = aRow;
}
return;
}
// Look to see if this row has a Parent
if(aRow.HasParent(aRow.Band.ParentBand) == true)
this.ultraGrid1.ActiveRow = aRow.ParentRow;
下滚一行
if(this.ultraGrid1.ActiveRow == null)
return;
// Retrieve reference to current ActiveRow
UltraGridRow aRow = this.ultraGrid1.ActiveRow;
// Look to see if this row has a child row
if(aRow.HasChild() == true)
{
aRow = aRow.GetChild(ChildRow.First);
this.ultraGrid1.ActiveRow = aRow;
return;
}
// Look to see if this row has a next sibling
if(aRow.HasNextSibling() == true)
{
aRow = aRow.GetSibling(SiblingRow.Next);
this.ultraGrid1.ActiveRow = aRow;
return;
}
// Look for next sibling of parent
aRow = aRow.ParentRow;
if(aRow != null)
{
if(aRow.HasNextSibling() == true)
{
aRow = aRow.GetSibling(SiblingRow.Next);
this.ultraGrid1.ActiveRow = aRow;
return;
}
}
if(aRow != null)
this.ultraGrid1.ActiveRow = aRow;
滚到最后一行
if(this.ultraGrid1.ActiveRow == null)
return;
// Retrieve last row in band(0)
UltraGridRow aRow = this.ultraGrid1.GetRow(ChildRow.Last);
// Look for last child of last child
while(aRow.HasChild() != false)
{
aRow = aRow.GetChild(ChildRow.Last);
}
// Back up as many as three rows so we can see what is above this row
UltraGridRow topRow = aRow;
int intCount = 3;
while(intCount <= 0 || topRow.HasPrevSibling() == false)
{
intCount -= 1;
topRow = topRow.GetSibling(SiblingRow.Previous);
}
// Set topRow as active row to position it to top of Grid
this.ultraGrid1.ActiveRow = topRow;
// Set aRow as Active Row
this.ultraGrid1.ActiveRow = aRow;
滚动到父band的首行
private void ultraGrid1_BeforeRowActivate(object sender, RowEventArgs e)
{
// Determine which row should be the top row
UltraGridRow rowTop;
if(e.Row.Band.Index == 0)
rowTop = e.Row;
else
rowTop = e.Row.ParentRow;
// Scroll the row into RowScrollRegion
this.ultraGrid1.DisplayLayout.RowScrollRegions[0].ScrollRowIntoView(rowTop);
// Turn off display refresh during update
this.ultraGrid1.BeginUpdate();
// Scroll the top row to the top
while(this.ultraGrid1.DisplayLayout.RowScrollRegions[0].VisibleRows[0].Row != rowTop)
{
this.ultraGrid1.DisplayLayout.RowScrollRegions[0].Scroll(
RowScrollAction.LineDown);
}
// Turn display refresh back on
this.ultraGrid1.EndUpdate();
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UltraBid.DAL;
using Infragistics.Win;
using Infragistics.Win.UltraWinDataSource;
using Infragistics.Win.UltraWinGrid;
namespace UltraBid.Controls
{
public partial class BidCompanyForm : UserControl, IShowBidInfo
{
Bid _bid;
IList _list;
public void ShowInfo(Bid bid)
{
_bid = bid;
if (_bid != null)
{
_list = BidPaper.ListByBidId(bid.BidId);
ultraDataSource.Rows.SetCount(_list.Count);
this.grid.DataSource = ultraDataSource;
//this.grid.SetDataBinding(_list, "", true);
}
}
//---------------------------------------
// Init
//---------------------------------------
public BidCompanyForm()
{
InitializeComponent();
}
private void BidCompanyForm_Load(object sender, EventArgs e)
{
}
//-----------------------------------------------------
// ultraDataSource
//-----------------------------------------------------
private void ultraDataSource_RowAdding(object sender, RowAddingEventArgs e)
{
BidPaper paper = new BidPaper();
paper.BidId = _bid.BidId;
_list.Add(paper);
}
private void ultraDataSource_RowDeleting(object sender, RowDeletingEventArgs e)
{
(_list[e.Row.Index] as BidPaper).Remove();
_list.RemoveAt(e.Row.Index);
}
private void ultraDataSource_CellDataRequested(object sender, CellDataRequestedEventArgs e)
{
BidPaper paper = _list[e.Row.Index] as BidPaper;
if (paper == null) return;
switch (e.Column.Key)
{
case "BidPaperId":
e.Data = paper.BidPaperId;
break;
case "BidId":
e.Data = paper.BidId;
break;
case "BidCompanyName":
e.Data = paper.BidCompanyName;
break;
case "BidCompanyAsset":
e.Data = paper.BidCompanyAsset;
break;
case "BidManager":
e.Data = paper.BidManager;
break;
case "BidContact":
e.Data = paper.BidContact;
break;
case "BidTel":
e.Data = paper.