Infragistics - UltraGrid - 图文

更新时间:2023-12-21 15:06:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

myl0197

电脑是我的老婆,编程是我的灵魂,代码是我的语言,按键是我在歌唱。(转贴)

? ? ? ? ? ? ?

博客园 社区 首页 新随笔 联系 管理 订阅

随笔- 19 文章- 0 评论- 9

Infragistics netadvantage UltraGrid (UltraWinGrid) 编程手记(转载Kevin Cheng's Yard)

UltraGrid 是Infragistics netadvanage 控件库中提供的一个Windows 网格控件,功能强大,完全可以取代VS 中提供的 GridView控件。但不知为何,国内介绍它的文章很少。这玩意功能是相当强大,但其属性设计原理和普通控件不太一样,属性极为复杂,没有手册几乎无法编码。在此我整理了一些资料,权当推广了。本是编程用的参考,文笔随意之处望见谅。kevin cheng 2008-08-20 参考

提供商首页:http://www.infragistics.com/ 术语

persist 持续化(保存) property 属性 attribute 特性 setting 设置 Band 数据条带 示例图片

一、UltraGrid 的基本概念

UltraGrid的属性设计和一般的控件颇为不同。理解其设计思想,有助于你从其浩瀚的属性中快速找到你所需要的东西。在此文中 WinGrid, UltraGrid, UltraWinGrid 是同一个东西。

HotTrack

this.ultraGrid1.DisplayLayout.Override.HotTrackCellAppearance.BackColor = Color.Blue;

this.ultraGrid1.DisplayLayout.Override.HotTrackRowCellAppearance.BackColor =

Color.Yellow;

this.ultraGrid1.DisplayLayout.Override.HotTrackHeaderAppearance.BackColor = Color.Blue;

this.ultraGrid1.DisplayLayout.Override.HotTrackRowAppearance.ForeColor = Color.LightGreen;

this.ultraGrid1.DisplayLayout.Override.HotTrackRowSelectorAppearance.BackColor = Color.Green;

分割滚动视图

设置分割视图(创建SplitBar)

this.ultraGrid1.DisplayLayout.ColScrollRegions[0].Split(0); this.ultraGrid1.DisplayLayout.RowScrollRegions[0].Split(200); 取消分割视图

this.ultraGrid1.DisplayLayout.MaxColScrollRegions = 1; this.ultraGrid1.DisplayLayout.MaxRowScrollRegions = 1;

修改Tab键功能

this.ultraGrid1.DisplayLayout.TabNavigation = TabNavigation.NextControl;

TabletPC Ink-Enable

将UltraInkProvider 组件拖到窗体上就可以了

编辑状态的输入框右侧会显示一个小笔按钮,点击后就会弹出手写输入框 若要在非TabletPC上使用该功能,需要安装TabletPC SDK

定义和应用外观样式

this.ultraGrid1.DisplayLayout.Appearances.Add(\

this.ultraGrid1.DisplayLayout.Appearances[\= Color.Red;

this.ultraGrid1.DisplayLayout.Appearances[\= Color.

White;

this.ultraGrid1.DisplayLayout.Override.ActiveRowAppearance = this.ultraGrid1.DisplayLayout.Appearances[\

this.ultraGrid1.DisplayLayout.Override.RowSelectorAppearance = this.ultraGrid1.DisplayLayout.Appearances[\ed\ --------------------------

this.ultraGrid1.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.Red;

this.ultraGrid1.DisplayLayout.Override.ActiveRowAppearance.ForeColor = Color.White;

this.ultraGrid1.DisplayLayout.Override.RowSelectorAppearance.BackColor = Color.Red;

this.ultraGrid1.DisplayLayout.Override.RowSelectorAppearance.ForeColor = Color.White;

GroupBy 视图

this.ultraGrid1.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy; this.ultraGrid1.DisplayLayout.Bands[0].SortedColumns.Add(\false, true);

this.ultraGrid1.DisplayLayout.Bands[0].SortedColumns.Add(\false, true); -------------------------------------------------------

this.ultraGrid1.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;

this.ultraGrid1.DisplayLayout.GroupByBox.Style = GroupByBoxStyle.Compact; this.ultraGrid1.DisplayLayout.GroupByBox.Appearance.BackColor = Color.White;

this.ultraGrid1.DisplayLayout.GroupByBox.Prompt = \and drop a column to group by that column.\

this.ultraGrid1.DisplayLayout.GroupByBox.PromptAppearance.ForeColor = Color.Maroon;

this.ultraGrid1.DisplayLayout.GroupByBox.BorderStyle = UIElementBorderStyle.InsetSoft;

this.ultraGrid1.DisplayLayout.GroupByBox.ButtonBorderStyle = UIElementBorderStyle.RaisedSoft;

this.ultraGrid1.DisplayLayout.GroupByBox.ShowBandLabels = ShowBandLabel

s.All;

this.ultraGrid1.DisplayLayout.GroupByBox.BandLabelBorderStyle = UIElementBorderStyle.Solid;

this.ultraGrid1.DisplayLayout.GroupByBox.BandLabelAppearance.BackColor = Color.DarkBlue;

this.ultraGrid1.DisplayLayout.GroupByBox.BandLabelAppearance.ForeColor = Color.LightYellow;

this.ultraGrid1.DisplayLayout.GroupByBox.ButtonConnectorStyle = UIElementBorderStyle.Dotted;

this.ultraGrid1.DisplayLayout.GroupByBox.ButtonConnectorColor = Color.Maroon; 卡片视图

grid.Band[0].CardView = true; grid.Band[0].CardSettings..... 样式设置 展开所有行(子行)

this.ultraGrid1.Rows.ExpandAll(true); 数据过滤器 示例

e.Layout.Override.AllowRowFiltering = DefaultableBoolean.True; e.Layout.Override.RowFilterAction = RowFilterAction.AppearancesOnly; e.Layout.Override.FilteredInCellAppearance.ForeColor = Color.DarkGreen; e.Layout.Override.FilteredOutCellAppearance.ForeColor = Color.DarkRed; 打开过滤器

this.ultraGrid1.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True; 设置过滤器功能

this.ultraGrid1.DisplayLayout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;

this.ultraGrid1.DisplayLayout.Override.FilterUIType = FilterUIType.FilterRow;

this.ultraGrid1.DisplayLayout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnLeaveCell;

this.ultraGrid1.DisplayLayout.Override.FilterOperatorDefaultValue =

FilterOperatorDefaultValue.DoesNotContain;

this.ultraGrid1.DisplayLayout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

this.ultraGrid1.DisplayLayout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

this.ultraGrid1.DisplayLayout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

this.ultraGrid1.DisplayLayout.Override.FilterClearButtonLocation = FilterClearButtonLocation.Row;

this.ultraGrid1.DisplayLayout.Override.FilterRowPrompt = \here to filter rows...\

this.ultraGrid1.DisplayLayout.Bands[0].SpecialRowPromptField = \ the LogicalOperator property of the ColumnFiltersCollection object. 外观

ultraGrid1.DisplayLayout.Override.FilterRowAppearance ultraGrid1.DisplayLayout.Override.FilterRowAppearanceActive ultraGrid1.DisplayLayout.Override.FilterCellAppearance

ultraGrid1.DisplayLayout.Override.FilterRowSelectorAppearance 自定义过滤规则

UltraGridBand band = this.ultraGrid1.DisplayLayout.Bands[2]; band.ColumnFilters[\Price\); band.ColumnFilters[\Price\ FilterComparisionOperator.GreaterThan, 5 );

band.ColumnFilters[\Price\ FilterComparisionOperator.LessThan, 10 );

band.ColumnFilters[\Price\= FilterLogicalOperator.And;

数据错误提示

this.ultraGrid1.DisplayLayout.Override.SupportDataErrorInfo = SupportDataErrorInfo.RowsAndCells;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].SupportDataErrorInfo =

DefaultableBoolean.False;

this.ultraGrid1.DisplayLayout.Override.DataErrorCellAppearance.BackColor = Color.Red;

this.ultraGrid1.DisplayLayout.Override.DataErrorRowSelectorAppearance.BackColor = Color.Red;

this.ultraGrid1.DisplayLayout.Override.DataErrorRowAppearance.BackColor = Color.LightYellow;

private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) {

string rowError = \ string cellError = \

Object value = e.Row.Cells[\

// Set the data error if Fax column value is empty if(DBNull.Value == value) {

rowError = \contains errors.\ cellError = \can not be empty\ }

DataRowView drv = (DataRowView)e.Row.ListObject; drv.Row.RowError = rowError;

drv.Row.SetColumnError(\cellError); }

Tooltip

SummarySettings averageSummary = e.Layout.Bands[0].Summaries.Add( \

SummaryType.Average,

e.Layout.Bands[0].Columns[\);

averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;

averageSummary.SummaryPositionColumn = averageSummary.SourceColumn; averageSummary.DisplayFormat = \{0:.##}\

averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;

averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;

---------------------------------------- SummarySettings summary =

this.ultraGrid1.DisplayLayout.Bands[0].Summaries.Add( \

SummaryType.Sum,

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0], SummaryPosition.UseSummaryPositionColumn );

summary.ToolTipText = string.Format( \ of column {0}.\ summary.SourceColumn.Header.Caption );

this.ultraGrid1.Rows.SummaryValues[ \].ToolTipText = string.Format( %um of

column {0}.\summary.SourceColumn.Header.Caption );

AlphaBend

if(this.chkRowAlpha.Checked == true) {

this.ultraGrid1.DisplayLayout.Override.RowAppearance.BackColorAlpha = Alpha.UseAlphaLevel;

this.ultraGrid1.DisplayLayout.Override.RowAppearance.AlphaLevel = short.Parse(this.spnAlphaLevel.Value.ToString()); } else {

this.ultraGrid1.DisplayLayout.Override.RowAppearance.BackColorAlpha = Alpha.Opaque;

this.ultraGrid1.DisplayLayout.Override.RowAppearance.AlphaLevel = 0; }

------------------------------------------ if (chEnableAlphaBlending.Checked) {

this.ultraGrid1.DisplayLayout.Appearance.ImageBackground =

Image.FromFile(clsSamplesPath.GraphicsPath + \ontage.jpg\

this.ultraGrid1.DisplayLayout.Appearance.ImageBackgroundOrigin = Infragistics.Win.ImageBackgroundOrigin.Form;

this.ultraGrid1.DisplayLayout.Appearance.ImageBackgroundStyle = Infragistics.Win.ImageBackgroundStyle.Tiled;

this.ultraGrid1.AlphaBlendMode = Infragistics.Win.AlphaBlendMode.Optimized; } else {

this.ultraGrid1.AlphaBlendMode = Infragistics.Win.AlphaBlendMode.Disabled; this.ultraGrid1.DisplayLayout.Appearance.ImageBackground = null; } 显隐标题

if (chShowCaption.Checked == true)

this.ultraGrid1.Text = \CardView Sample\ else

this.ultraGrid1.Text = \ 边框显示

ug.DisplayLayout.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid; 背景色

ug.DisplayLayout.Appearance.BackColor = Color.White;

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ Band ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 设置数据条带退格宽度

this.ultraGrid1.DisplayLayout.Bands[1].Indentation = 100;

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ Caption ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 表格标题(Caption)

this.ultraGrid1.DisplayLayout.CaptionVisible = DefaultableBoolean.True;

标题

ug.DisplayLayout.CaptionAppearance.TextHAlign = Infragistics.Win.HAlign.Left;

ug.DisplayLayout.CaptionAppearance.BackColor = Color.LightSteelBlue;

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ Head ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 控制标题行的位置

// RepeatOnBreak, FixedOnTop, OncePerRowIsland, OncePerGroupedRowIsland

e.Layout.Override.HeaderPlacement = HeaderPlacement.OncePerGroupedRowIsland;

e.Layout.Bands[0].Override.HeaderPlacement = HeaderPlacement.FixedOnTop; 标题换行展示

this.ultraGrid1.DisplayLayout.Override.WrapHeaderText = true;

Header置顶不动

设置displaylayout中的stationarymargins 标题行样式

this.ultraGrid1.DisplayLayout.Override.AllowColSizing = AllowColSizing.Free; this.ultraGrid1.DisplayLayout.Bands[0].HeaderVisible = true; this.ultraGrid1.DisplayLayout.Bands[1].HeaderVisible = true; this.ultraGrid1.DisplayLayout.Bands[1].Header.Caption = \ this.ultraGrid1.DisplayLayout.Bands[0].Indentation = 0; this.ultraGrid1.DisplayLayout.Bands[1].Indentation = 0;

this.ultraGrid1.DisplayLayout.RowConnectorStyle = RowConnectorStyle.None; this.ultraGrid1.DisplayLayout.Bands[0].Header.Appearance.ThemedElementAlpha =

Alpha.Transparent;

this.ultraGrid1.DisplayLayout.Bands[0].Header.Appearance.BackColor = SystemColors.ActiveCaption;

this.ultraGrid1.DisplayLayout.Bands[0].Header.Appearance.ForeColor =

SystemColors.ActiveCaptionText;

this.ultraGrid1.DisplayLayout.Bands[1].Header.Appearance.ThemedElementAlpha =

Alpha.Transparent;

this.ultraGrid1.DisplayLayout.Bands[1].Header.Appearance.BackColor = Color.Blue;

this.ultraGrid1.DisplayLayout.Bands[1].Header.Appearance.BackColor2 = Color.Red;

this.ultraGrid1.DisplayLayout.Bands[1].Header.Appearance.ForeColor = Color.White;

this.ultraGrid1.DisplayLayout.Bands[1].Header.Appearance.BackGradientStyle =

GradientStyle.Horizontal; 隐藏标题行

this.UltraGrid1.Text = \

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ Row ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 行选择方式

e.Layout.Override.SelectTypeRow = SelectType.Single;

e.DisplayLayout.Bands[\= SelectType.Extended;

e.Layout.Override.CellClickAction = CellClickAction.RowSelect; 选择行样式

ug.DisplayLayout.Override.SelectedRowAppearance.BackColor = Color.SteelBlue; 交错行样式

this.ultraGrid1.DisplayLayout.Override.RowAlternateAppearance.BackColor = Color.Red; 激活行样式

this.ultraGrid1.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.Red;

ug.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.SteelBlue;

添加行操作提示

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

this.ultraGrid1.DisplayLayout.Bands[0].AutoPreviewEnabled = true; }

private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) {

e.Row.Description = \Description\ }

private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e) {

this.ultraGrid1.DisplayLayout.ActiveRow.Description =

\changed in this row will not be added to the database\+ %until you press the Update button.\ } 行渐变背景

UltraGridRow objRow = this.ultraGrid1.Rows[10]; this.ultraGrid1.ActiveRow = objRow;

this.ultraGrid1.Rows[10].Appearance.BackColor2 = Color.Blue;

this.ultraGrid1.Rows[10].Appearance.BackGradientStyle = GradientStyle.Circular;

修改特殊行的位置 新增行

this.ultraGrid1.DisplayLayout.Override.AllowAddNew = AllowAddNew.TemplateOnTop;

this.ultraGrid1.DisplayLayout.Override.SequenceFixedAddRow = 2; 过滤行

this.ultraGrid1.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True;

this.ultraGrid1.DisplayLayout.Override.FilterUIType = FilterUIType.FilterRow;

this.ultraGrid1.DisplayLayout.Override.SequenceFilterRow = 1; 统计行

this.ultraGrid1.DisplayLayout.Override.AllowRowSummaries = AllowRowSummaries.True;

this.ultraGrid1.DisplayLayout.Override.SummaryDisplayArea = SummaryDisplayAreas.Top;

this.ultraGrid1.DisplayLayout.Override.SequenceSummaryRow = 3; 行高度调整

e.Layout.Override.DefaultRowHeight = 60; e.Layout.Override.RowSizing = RowSizing.Free;

this.ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.Free;

行布局(将列分组展示)

ultraGrid1.DisplayLayout.Bands[0].Groups.Add(\ ultraGrid1.DisplayLayout.Bands[0].Groups.Add(\ ultraGrid1.DisplayLayout.Bands[0].Groups.Add(\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\e\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\erID\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\=

ultraGrid1.DisplayLayout.Bands[0].Groups[\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= ultraGrid1.DisplayLayout.Bands[0].Groups[\ ultraGrid1.DisplayLayout.Bands[0].LevelCount = 2;

ultraGrid1.DisplayLayout.Bands[0].Columns[\= 1; ultraGrid1.DisplayLayout.Bands[0].Columns[\= 1; ultraGrid1.DisplayLayout.Bands[0].Columns[\= 1; ultraGrid1.DisplayLayout.Bands[0].Columns[\= 1; 冻结行

this.ultraGrid1.DisplayLayout.Override.FixedRowStyle = FixedRowStyle.Top; this.ultraGrid1.Rows.FixedRows.Add(this.ultraGrid1.Rows[0]); this.ultraGrid1.Rows[0].Fixed = true;

this.ultraGrid1.DisplayLayout.Override.FixedRowIndicator = FixedRowIndicator.Button;

FixedRowAppearance FixedRowCellAppearance FixedRowSelectorAppearance 统计行

this.ultraGrid1.DisplayLayout.Override.AllowRowSummaries = AllowRowSummaries.True;

this.ultraGrid1.DisplayLayout.Override.SummaryDisplayArea =

SummaryDisplayAreas.GroupByRowsFooter | SummaryDisplayAreas.TopFixed; ------------------------------------------------------

this.ultraGrid1.DisplayLayout.Bands[0].Summaries.Add( SummaryType.Minimum, null,

this.ultraGrid1.DisplayLayout.Bands[0].Columns[\ SummaryPosition.Left, null );

this.ultraGrid1.DisplayLayout.Bands[0].Summaries[0].SummaryPosition = SummaryPosition.Center;

this.ultraGrid1.DisplayLayout.Bands[0].SummaryFooterCaption = \Sums\

行首显示行号(行选择器)

e.Layout.Override.RowSelectors = DefaultableBoolean.True;

e.Layout.Override.RowSelectorNumberStyle = RowSelectorNumberStyle.VisibleIndex;

e.Layout.Override.RowSelectorWidth = 30; 选中行

this.ultraGrid1.Rows[i].Selected = true; 激活行

this.ultraGrid1.Rows[i].Activate();

this.ultraGrid1.ActiveRow = this.ultraGrid1.Rows[5];

this.ultraGrid1.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.Aqua;

this.ultraGrid1.ActiveCell = this.ultraGrid1.ActiveRow.Cells[\

遍历行

// 定位到首行

UltraGridRow aRow = this.ultraGrid1.GetRow(ChildRow.First); // 搜索后继行

if(aRow.HasNextSibling()) {

UltraGridRow NextRow = aRow.GetSibling(SiblingRow.Next); MessageBox.Show(\Siblings\ }

// 搜索子行

if(aRow.HasChild()) {

UltraGridRow childRow = aRow.GetChild(Infragistics.Win.UltraWinGrid.ChildRow.First);

MessageBox.Show(\a Child\ } 判断总行数

this.ultraGrid1.Rows.Count

特殊行外观设置

this.ultraGrid1.DisplayLayout.Override.SpecialRowSeparator =

SpecialRowSeparator.FixedRows;

this.ultraGrid1.DisplayLayout.Override.SpecialRowSeparatorHeight = 25; this.ultraGrid1.DisplayLayout.Override.BorderStyleSpecialRowSeparator = UIElementBorderStyle.Dashed; 隐藏行分隔线 e.Layout.Reset();

e.Layout.Override.CellAppearance.BorderAlpha = Alpha.Transparent; e.Layout.Override.RowAppearance.BorderColor = Color.White;

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ Column ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ UltraGrid内置支持的列编辑类型 Default 缺省类型 Edit 文本框

EditButton 按钮文本框 CheckBox 复选框 TriStateCheckBox 三态复选框 DropDown 组合框(可编辑) DropDownList 下拉框(只选)

DropDownValidate 组合框(只可输入列表中的数据) Button 按钮。点击时触发 ClickCellButton 事件 DropDownCalendar 下拉日历控件

Date 日期输入、下拉日历组合控件。可设置 MinValue 和 MaxValue,并对

输入进行掩码校验

DateWithoutDropDown 日期输入

DateWithSpin 类似Date控件,但用spin按钮取代了下拉日历控件 DateTime 日期时间输入控件

DateTimeWithoutDropDown 日期时间输入控件,但取消了下拉按钮 DateTimeWithSpin 日期时间输入控件,使用spin按钮替代下拉按钮 Color 色彩选择

Currency 金额,可修改 MaskInput 属性定制mask CurrencyNonNegative 金额,不允许负数 CurrencyPositive 金额,只允许正数

Double double。可用 NumberFormatInfo 设置掩码,用 MinValue MaxValue

限制范围

DoubleWithSpin double + spin DoubleNonNegative double 非负 DoubleNonNegativeWithSpin double 非负 spin DoublePositive double 正数 DoublePositiveWithSpin double 正数 spin Font 字体选择框

Image 图像展示(EmbeddableImageRenderer) ImageWithShadow 图像,阴影效果 Integer 整数

IntegerWithSpin 整数,spin IntegerPositive 整数,正数

IntegerPositiveWithSpin 整数,正数,spin IntegerNonNegative 整数,非负

IntegerNonNegativeWithSpin 整数,非负,spin

Time 时间(可用MaskInput 设置掩码,用 Minimum 和 maximum 设置范围 )

TimeWithSpin 时间,spin TimeZone 时区

URL URL(FormattedLinkEditor)

FormattedText 带格式的文本(FormattedLinkEditor)

设置列编辑类型示例 button

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Style = ColumnStyle.Button; checkbox

this.ultraGrid1.DisplayLayout.Bands[0].Columns.Add(\ this.ultraGrid1.DisplayLayout.Bands[0].Columns[\ype = typeof (bool);

this.ultraGrid1.DisplayLayout.Bands[0].Columns[\=

ColumnStyle.CheckBox; dropdownlist

this.ultraGrid1.DisplayLayout.ValueLists.Add(\

this.ultraGrid1.DisplayLayout.ValueLists[\s.Add(1, \e\

this.ultraGrid1.DisplayLayout.ValueLists[\s.Add(2, \o\

this.ultraGrid1.DisplayLayout.ValueLists[\s.Add(3, \ee\

this.ultraGrid1.DisplayLayout.ValueLists[\= ValueListDisplayStyle.DisplayText;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[2].Style = ColumnStyle.DropDownList;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[2].ValueList = this.ultraGrid1.DisplayLayout.ValueLists[\ 手工设置单元格编辑控件

EmbeddableEditorBase editor = null;

DefaultEditorOwnerSettings editorSettings = new DefaultEditorOwnerSettings( );

--------------------------------------- EditorWithText

editorSettings.DataType = typeof( string );

editor = new EditorWithText( new DefaultEditorOwner( editorSettings ) ); ColorPickerEditor

editorSettings.DataType = typeof( Color );

editor = new ColorPickerEditor( new DefaultEditorOwner( editorSettings ) ); FontNameEditor

valueList = new ValueList( );

editorSettings.DataType = typeof( string );

for ( int i = 0; i < System.Drawing.FontFamily.Families.Length; i++ ) valueList.ValueListItems.Add( System.Drawing.FontFamily.Families[i].Name );

editorSettings.ValueList = valueList;

editor = new FontNameEditor( new DefaultEditorOwner( editorSettings ) ); --------------------------------------- EditorWithMarsk (Currency)

editorSettings.DataType = typeof( decimal );

editor = new EditorWithMask( new DefaultEditorOwner( editorSettings ) ); editorSettings.MaskInput = \-nn,nnn,nnn.nn\

EditorWithMask (double)

editorSettings.DataType = typeof( double ); editorSettings.MaskInput = \

editor = new EditorWithMask( new DefaultEditorOwner( editorSettings ) );

EditorWithMask(integers)

editorSettings.DataType = typeof( int );

editor = new EditorWithMask( new DefaultEditorOwner( editorSettings ) ); editorSettings.MaskInput = \ EditorWithMask(IPv4)

editorSettings.DataType = typeof( string );

editor = new EditorWithMask( new DefaultEditorOwner( editorSettings ) ); editorSettings.MaskInput = \ EditorWithMask(phone numbers)

editorSettings.DataType = typeof( string );

editor = new EditorWithMask( new DefaultEditorOwner( editorSettings ) ); editorSettings.MaskInput = \###-####\ EditorWithMask (hh:mms:ss tt)

editorSettings.DataType = typeof( DateTime ); editorSettings.MaskInput = \tt\

editor = new EditorWithMask( new DefaultEditorOwner( editorSettings ) ); --------------------------------------- EditorWithCombo (Yes No dropdownlist) editorSettings.DataType = typeof( bool ); valueList = new ValueList( );

valueList.ValueListItems.Add( true, \); valueList.ValueListItems.Add( false, \); editorSettings.ValueList = valueList;

editor = new EditorWithCombo( new DefaultEditorOwner( editorSettings ) ); EditorWithCombo (using ValueList) valueList = new ValueList( );

valueList.ValueListItems.Add( 0, \); valueList.ValueListItems.Add( 1, \); valueList.ValueListItems.Add( 2, \); valueList.ValueListItems.Add( 3, \); valueList.ValueListItems.Add( 4, \); valueList.ValueListItems.Add( 5, \); editorSettings.ValueList = valueList;

editorSettings.DataType = typeof( int );

editor = new EditorWithCombo( new DefaultEditorOwner( editorSettings ) ); EditorWithCombo (using UltraDropDown) // UltraDropDown : IValueList

UltraDropDown dropDown = new UltraDropDown(); dropDown.Visible = false;

dropDown.DataSource = new Record[]{ new Record(\1\ new Record(\2\ new Record(\3\ new Record(\4\ };

dropDown.ValueMember = \// Cell values of this column are used as data.

dropDown.DisplayMember = \// Cell values of this column are dispalyed in the cells.

dropDown.DisplayLayout.Override.CellAppearance.BackColor = Color.LightYellow;

dropDown.DisplayLayout.Override.CellAppearance.BackColor2 = Color.Yellow; dropDown.DisplayLayout.Override.CellAppearance.BackGradientStyle = GradientStyle.ForwardDiagonal; this.Controls.Add(dropDown); //

editorSettings.ValueList = dropDown; editorSettings.DataType = typeof( int );

editor = new EditorWithCombo( new DefaultEditorOwner( editorSettings ) ); --------------------------------------- OptionSetEditor

editorSettings.DataType = typeof( bool ); valueList = new ValueList( );

valueList.ValueListItems.Add( true, \); valueList.ValueListItems.Add( false, \); editorSettings.ValueList = valueList;

editor = new OptionSetEditor( new DefaultEditorOwner( editorSettings ) ); --------------------------------------- CheckEditor

editorSettings.DataType = typeof( bool );

editor = new CheckEditor( new DefaultEditorOwner( editorSettings ) ); CheckEditor (ThreeState)

editorSettings.DataType = typeof( bool );

editor = new CheckEditor( new DefaultEditorOwner( editorSettings ) ); ((CheckEditor)editor).ThreeState = true; --------------------------------------- DateTimeEditor

editorSettings.DataType = typeof( DateTime ); editorSettings.MaskInput = \

editor = new DateTimeEditor( new DefaultEditorOwner( editorSettings ) ); DateTimeEditor (mm/dd/yy hh:mm:ss)

editorSettings.DataType = typeof( DateTime ); editorSettings.MaskInput = \hh:mm:ss\

editor = new DateTimeEditor( new DefaultEditorOwner( editorSettings ) ); --------------------------------------- ProgressBarEditor

editorSettings.DataType = typeof( int );

editor = new Infragistics.Win.UltraWinProgressBar.ProgressBarEditor( new DefaultEditorOwner( editorSettings ) ); 设置 FieldValue 列的控件

private void ultraGrid2_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e) {

string fieldType = e.Row.Cells[\ EmbeddableEditorBase editor = ...

e.Row.Cells[ \].Editor = editor; } 列只读

e.Layout.Override.Rows[\= CellActivation.NoEdit; 隐藏列

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true; 冻结列

this.ultraGrid1.DisplayLayout.UseFixedHeaders = true;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Header.Fixed = true;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Header.FixedHeaderIndicator =

FixedHeaderIndicator.None;

this.ultraGrid1.DisplayLayout.Override.FixedHeaderAppearance.BackColor = Color.LightYellow;

this.ultraGrid1.DisplayLayout.Override.FixedCellAppearance.BackColor = Color.LightYellow;

this.ultraGrid1.DisplayLayout.Override.FixedCellSeparatorColor = Color.Red; 展示列分隔线

this.ultraGrid1.DisplayLayout.Override.CellAppearance.BorderColor = Color.Red; 列允许拖动和切换

this.ultraGrid1.DisplayLayout.Override.AllowColMoving = AllowColMoving.WithinBand;

this.ultraGrid1.DisplayLayout.Override.AllowColSwapping = AllowColSwapping.WithinBand; 列宽调整

e.Layout.AutoFitStyle = AutoFitStyle.ExtendLastColumn; e.Layout.Override.AllowColSizing = AllowColSizing.Free; this.ultraGrid1.DisplayLayout.Override.ColumnAutoSizeMode = ColumnAutoSizeMode.VisibleRows;

this.ultraGrid1.DisplayLayout.Bands[1].Override.ColumnAutoSizeMode = ColumnAutoSizeMode.AllRowsInBand;

this.ultraGrid1.Rows[0].PerformAutoSize( ); 遍历列

grid.Layout.Bands[0].Columns.FromKey(\

foreach ( UltraGridColumn column in e.Layout.Bands[0].Columns ) column.Width = 100; 判断某列是否重复

public static bool CheckIfSame(UltraGrid ug, string colname) {

for(int i = 0; i < ug.Rows.Count; i++) {

for(int j = i+1; j < ug.Rows.Count; j++)

if(ug.Rows[i].Cells[colname].Text == ug.Rows[j].Cells[colname].Text &&

ug.Rows[i].Cells[colname].Text != \ return true; }

return false; } 添加非绑定列

this.ultraGrid1.DisplayLayout.Bands[0].Columns.Add(\\culated Value\

获取子数据条带个数

private int GetNumberofChildBands(Infragistics.Win.UltraWinGrid.UltraGridBand aBand) {

int GetNumberofChildBands = 0;

foreach(UltraGridColumn aCol in aBand.Columns) {

if(aCol.IsChaptered)

GetNumberofChildBands = GetNumberofChildBands + 1; }

return GetNumberofChildBands; }

不同值可以给以不同的颜色。

Properties-> DisplayLayout->bands->0-XXX->Columns->从弹出窗口中,选择一个列-

>ValueBaseAppearance->添加Conditions,即可。

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■ Cell ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 选中单元格样式

ug.DisplayLayout.Override.SelectedCellAppearance.BackColor = Color.SteelBlue;//选中单元格 单元格选择方式

e.Layout.Override.SelectTypeCell = SelectType.Single;

单元格格式

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

// Fit columns

e.Layout.AutoFitStyle = AutoFitStyle.ExtendLastColumn;

// Create culture objects

CultureInfo cultureENUS = CultureInfo.CreateSpecificCulture(\ CultureInfo cultureDEDE = CultureInfo.CreateSpecificCulture(\ CultureInfo cultureJAJP = CultureInfo.CreateSpecificCulture(\

// Set date formats

e.Layout.Bands[0].Columns[\= \

e.Layout.Bands[0].Columns[\= cultureENUS; e.Layout.Bands[0].Columns[\= \

e.Layout.Bands[0].Columns[\= cultureDEDE; e.Layout.Bands[0].Columns[\= \

e.Layout.Bands[0].Columns[\= cultureJAJP; }

文字过长的裁剪展示方式

this.ultraGrid1.DisplayLayout.Override.CellAppearance.TextTrimming = TextTrimming.EllipsisCharacter;

修改单元格背景

void UltraGrid1_AfterCellUpdate(...) {

if(e.Cell.Column.Key == \ {

int cellValue = (int)e.Cell.Value; if(cellValue == 100) {

e.Cell.Appearance.BackColor = System.Drawing.Color.Red; }

else

e.Cell.Appearance.BackColor = System.Drawing.Color.White; } }

单元格背景图片

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].CellAppearance.ImageBackground =

Image.FromFile(System.IO.Path.Combine(

Application.StartupPath, @\in Field.bmp\ }

单元格按钮样式

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Style = ColumnStyle.Button; this.ultraGrid1.DisplayLayout.Override.CellButtonAppearance.Image = Image.FromFile(System.IO.Path.Combine(

Application.StartupPath, @\in Field.bmp\ this.ultraGrid1.DisplayLayout.Bands[0].Override.ButtonStyle = UIElementButtonStyle.Button3D;

多行单元格

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].CellMultiLine = DefaultableBoolean.True;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].VertScrollBar = true; this.ultraGrid1.DisplayLayout.Override.DefaultRowHeight = 100; 单元格点击处理方式

this.ultraGrid1.DisplayLayout.Override.CellClickAction = CellClickAction.EditAndSelectText;

单元格值和键

this.ultraGrid1.Rows[0].Cells[\字段名\// 单元格的值 cell.Column.Key // 单元格归属的列的键 this.ultraGrid1.Selected.Cells; // 被选中单元格

下拉框单元格 手工设置

在Properties-> DisplayLayout->ValueLists,添加ValueList

Properties-> DisplayLayout->bands->0-XXX->Columns->某个Column->ValueList等于新添加的

ValueList,同时设定Style是DropDown

还要再写个Event Handler,详细其他Style等等可以看这个Link:

http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.Aspx?ArticleID=1708 代码设置

ValueList locValueList = this.ultraGrid.DisplayLayout.ValueLists.Add(\ foreach (DataRow dr in m_ds.Tables[\

locValueList.ValueListItems.Add((int)(dr[\dr[\ ultraGrid.DisplayLayout.Bands[\er\= ultraGrid.DisplayLayout.ValueLists[\ ValueList 示例

ultraGrid1.DisplayLayout.ValueLists.Add(\

ultraGrid1.DisplayLayout.ValueLists[\s.Add(1, \ ultraGrid1.DisplayLayout.ValueLists[\s.Add(2, \ ultraGrid1.DisplayLayout.ValueLists[\s.Add(3, \ ultraGrid1.DisplayLayout.ValueLists[\= ValueListDisplayStyle.DisplayText;

ultraGrid1.DisplayLayout.Bands[0].Columns.Add(\

ultraGrid1.DisplayLayout.Bands[0].Columns[\= this.ultraGrid1.DisplayLayout.ValueLists[\ ultraGrid1.DataBind();

设置特定单元格处于编辑状态

this.ultraGrid1.ActiveCell = this.ultraGrid1.ActiveRow.Cells[\ this.ultraGrid1.Focus();

this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false); 合并单元格

合并具有相同值的单元格

this.ultraGrid1.DisplayLayout.Override.MergedCellStyle = MergedCellStyle.

Always;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].MergedCellStyle = MergedCellStyle.Never; 合并单元格外观设置

this.ultraGrid1.DisplayLayout.Override.MergedCellAppearance.BackColor = Color.LightYellow;

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].MergedCellAppearance.BackColor = Color.Blue;

合并逻辑:文本一致就可以合并

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].MergedCellEvaluationType =

MergedCellEvaluationType.MergeSameText; 定制单元格合并逻辑

private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) {

e.Layout.Override.MergedCellStyle = MergedCellStyle.Always;

e.Layout.Bands[0].Columns[\= new CustomMergedCellEvaluator(); }

public class CustomMergedCellEvaluator: Infragistics.Win.UltraWinGrid.IMergedCellEvaluator {

public CustomMergedCellEvaluator(){} // 合并日期相同的单元格(无需时间精确相同)

public bool ShouldCellsBeMerged(UltraGridRow row1, UltraGridRow row2,

UltraGridColumn column) {

DateTime date1 = (DateTime)row1.GetCellValue(column); DateTime date2 = (DateTime)row2.GetCellValue(column); return date1.Date == date2.Date; } }

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ 自定义单元格编辑控件 ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ EmbeddableEditorBase

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ FULL EXAMPLE ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ //

// UltraDataColumns //

UltraDataColumn ultraDataColumn1 = new UltraDataColumn(\ UltraDataColumn ultraDataColumn2 = new UltraDataColumn(\ UltraDataColumn ultraDataColumn3 = new UltraDataColumn(\me\

UltraDataColumn ultraDataColumn4 = new UltraDataColumn(\ UltraDataColumn ultraDataColumn5 = new UltraDataColumn(\\

UltraDataColumn ultraDataColumn6 = new UltraDataColumn(\ UltraDataColumn ultraDataColumn7 = new UltraDataColumn(\k\

UltraDataColumn ultraDataColumn8 = new UltraDataColumn(\ UltraDataColumn ultraDataColumn9 = new UltraDataColumn(\ UltraDataColumn ultraDataColumn10 = new UltraDataColumn(\e\

ultraDataColumn1.DataType = typeof(int); ultraDataColumn2.DataType = typeof(int); ultraDataColumn5.DataType = typeof(double); ultraDataColumn8.DataType = typeof(double); ultraDataColumn9.DataType = typeof(double); ultraDataColumn10.DataType = typeof(double); //

// UltraDataSource //

this.ultraDataSource = new Infragistics.Win.UltraWinDataSource.UltraDataSource

(this.components);

this.ultraDataSource.Band.Columns.AddRange(new object[] { ultraDataColumn1, ultraDataColumn2, ultraDataColumn3, ultraDataColumn4, ultraDataColumn5, ultraDataColumn6, ultraDataColumn7, ultraDataColumn8, ultraDataColumn9, ultraDataColumn10}); //

// UltraGridColumns //

UltraGridColumn ultraGridColumn1 = new UltraGridColumn(\ UltraGridColumn ultraGridColumn2 = new UltraGridColumn(\ UltraGridColumn ultraGridColumn3 = new UltraGridColumn(\me\

UltraGridColumn ultraGridColumn4 = new UltraGridColumn(\ ultraGridColumn1.CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;

ultraGridColumn1.Header.VisiblePosition = 0; ultraGridColumn1.Hidden = true;

ultraGridColumn2.CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;

ultraGridColumn2.Header.Caption = \编号\ ultraGridColumn2.Header.VisiblePosition = 1; ultraGridColumn2.Hidden = true;

ultraGridColumn3.CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;

ultraGridColumn3.Header.Caption = \投标公司\ ultraGridColumn3.Header.Fixed = true; ultraGridColumn3.Header.VisiblePosition = 2; ultraGridColumn3.Width = 159;

ultraGridColumn4.CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;

ultraGridColumn4.Header.Caption = \投标金额(万元)\ ultraGridColumn4.Header.VisiblePosition = 3; ultraGridColumn4.Width = 108; //

// UltraGridBand //

UltraGridBand ultraGridBand1 = new UltraGridBand(\0\-1); ultraGridBand1.Columns.AddRange(new object[] { ultraGridColumn1, ultraGridColumn2, ultraGridColumn3, ultraGridColumn4 }); //

// UltraGrid //

this.grid = new Infragistics.Win.UltraWinGrid.UltraGrid(); this.grid.DataSource = this.ultraDataSource; this.grid.DisplayLayout.AutoFitStyle =

Infragistics.Win.UltraWinGrid.AutoFitStyle.ExtendLastColumn;

四、UltraGrid 数据绑定

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■ Bind ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 绑定到IList(仅展示,不能添加删除)

this.ultraGrid1.SetDataBinding(myObjectList, \true); 绑定到平面数据源(DataTable)

private void ultraButton1_Click(object sender, System.EventArgs e) {

// Declare a DataTable to contain the program generated data DataTable dataTable = new DataTable(\

// Create and add a CustomerID column

DataColumn colWork = new DataColumn(\erID\System.Type.GetType

(\.Int32\

dataTable.Columns.Add(colWork);

// Add CustomerID column to key array and bind to DataTable DataColumn[] Keys = new DataColumn[1]; Keys[0] = colWork;

dataTable.PrimaryKey = Keys;

// Create and add a CustomerName column

colWork = new DataColumn(\erName\System.Type.GetType(\m.String\

colWork.MaxLength = 50;

dataTable.Columns.Add(colWork);

// Create and add a LastOrderDate column

colWork = new DataColumn(\System.Type.GetType(\m.DateTime\

dataTable.Columns.Add(colWork);

// Add a row

DataRow row = dataTable.NewRow(); row[\erID\= 1;

row[\erName\= \Widgets\ row[\= System.DateTime.Now; dataTable.Rows.Add(row);

// Add another row

row = dataTable.NewRow(); row[\erID\= 2;

row[\erName\= \Thingamagigs\

row[\= System.DateTime.Now.AddDays(-101); dataTable.Rows.Add(row);

// Bind the table to the grid

this.ultraGrid1.DataSource = dataTable; }

绑定到层级数据源(DataSet)

private void ultraButton1_Click(object sender, System.EventArgs e) {

// Declare DataSet to contain Hierarchical data // Make Customers DataTable // Make Orders DataTable

DataSet dataSet = new DataSet();

dataSet.Tables.Add(MakeCustomersDataTable());

dataSet.Tables.Add(MakeOrdersDataTable(dataSet.Tables[\ers\

// Create customers/orders relationship and add to DataSet DataRelation relCustOrder = new DataRelation( \

, dataSet.Tables[\ers\erID\ , dataSet.Tables[\ );

dataSet.Relations.Add(relCustOrder);

// Bind the DataSet to the Grid this.ultraGrid1.DataSource = dataSet; } // 客户表

private DataTable MakeCustomersDataTable() {

// Declare a DataTable to contain the program generated data DataTable dataTable = new DataTable(\

// Create and add a CustomerID column

DataColumn colWork = new DataColumn(\erID\System.Type.GetType

(\.Int32\

dataTable.Columns.Add(colWork);

// Add CustomerID column to key array and bind to DataTable DataColumn[] Keys = new DataColumn[1]; Keys[0] = colWork;

dataTable.PrimaryKey = Keys;

// Create and add a CustomerName column

colWork = new DataColumn(\erName\System.Type.GetType(\m.String\

colWork.MaxLength = 50;

dataTable.Columns.Add(colWork);

// Create and add a L=tOrderDate column

colWork = new DataColumn(\tOrderDate\System.Type.GetType(\.DateTime\

dataTable.Columns.Add(colWork);

// Add a row

DataRow row = dataTable.NewRow(); row[\erID\= 1;

row[\erName\= \Widgets\ row[\tOrderDate\= System.DateTime.Now; dataTable.Rows.Add(row);

// Add another row

row = dataTable.NewRow(); row[\erID\= 2;

row[\erName\= \Thingamagigs\

row[\tOrderDate\= System.DateTime.Now.AddDays(-101); dataTable.Rows.Add(row);

return dataTable; } // 订单表

private DataTable MakeOrdersDataTable(DataTable v_customersDataTable) {

// Declare a DataTable to contain the program generated data DataTable dataTable = new DataTable(\

// Create and add a CustomerID column

DataColumn colWork = new DataColumn(\erID\System.Type.GetType

(\.Int32\

dataTable.Columns.Add(colWork);

// Add CustomerID column to key array DataColumn[] Keys = new DataColumn[2]; Keys[0] = colWork;

// Create and add OrderID column

colWork = new DataColumn(\System.Type.GetType(\.String\

colWork.MaxLength = 15;

dataTable.Columns.Add(colWork);

// Add OrderID column to key array and bind to DataTable Keys[1] = colWork;

dataTable.PrimaryKey = Keys;

// Create and add a EmployeeID column

colWork = new DataColumn(\System.Type.GetType(\.Int32\

dataTable.Columns.Add(colWork);

// Create and add a OrderDate column

colWork = new DataColumn(\System.Type.GetType(\.DateTime\

dataTable.Columns.Add(colWork);

// Loop through Customer table and add Order rows foreach( DataRow custRow in v_customersDataTable.Rows) {

// Add four rows for each Customer DataRow row;

for(Int32 intPtr = 1; intPtr <= 4; intPtr++) {

row = dataTable.NewRow();

row[\= custRow[\erID\

row[\= intPtr * ((Int32)custRow[\erID\ row[\= intPtr * 10;

row[\= System.DateTime.Now.AddDays(intPtr); dataTable.Rows.Add(row); } }

return dataTable; }

■■■■■■■■■■■■■■■■■■■■■■■■■■

■■ 窗体控件数据同步 ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■ 与窗体控件同步数据

private void DataSourcesandWinGridRows_Load(object sender, System.EventArgs e) {

this.txtDiscount.DataBindings.Add(\\this.winGrid1.Order_Details, %unt\

this.txtQuantity.DataBindings.Add(\this.winGrid1.Order_Details, \tity\

this.txtUnitPrice.DataBindings.Add(\this.winGrid1.Order_Details, \Price\ }

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

e.Layout.Override.SelectTypeRow = SelectType.Single; e.Layout.Override.CellClickAction = CellClickAction.RowSelect; }

private void ultraGrid1_AfterSelectChange(object sender, AfterSelectChangeEventArgs e) {

// Use CurrencyManager to set position CurrencyManager currencyManager1;

currencyManager1 = (CurrencyManager)this.BindingContext[this.winGrid1.Order_Details];

currencyManager1.Position = this.ultraGrid1.Selected.Rows[0].Index; }

同步示例二

private void WinGridasaDataNavigator_Load(object sender, System.EventArgs e) {

// Set data binding for text boxes

this.txtCompanyName.DataBindings.Add(\\this.ultraGrid1.DataSource, \

this.txtContactName.DataBindings.Add(\\this.ultraGrid1.DataSource, \e\

this.txtContactTitle.DataBindings.Add(\\this.ultraGrid1.DataSource, \

this.txtAddress.DataBindings.Add(\\this.ultraGrid1.DataSource, \s\

this.txtCity.DataBindings.Add(\\this.ultraGrid1.DataSource, \ this.txtRegion.DataBindings.Add(\this.ultraGrid1.DataSource, \\

this.txtPostalCode.DataBindings.Add(\this.ultraGrid1.DataSource, \

this.txtCountry.DataBindings.Add(\\this.ultraGrid1.DataSource, \y\ }

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

// Hide all but \column

foreach(UltraGridColumn aColumn in e.Layout.Bands[0].Columns) {

switch (aColumn.Key) {

case \erID\

aColumn.CellActivation = Activation.NoEdit; break; default :

aColumn.Hidden = true; break; } }

// Configure grid

e.Layout.AutoFitStyle = AutoFitStyle.ResizeAllColumns; e.Layout.Override.SelectTypeCell = SelectType.None; e.Layout.Override.SelectTypeCol = SelectType.None; e.Layout.Override.SelectTypeRow = SelectType.None; }

■■■■■■■■■■■■■■■■■■■■■■■■■ ■■ 获取数据 ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■ 遍历行 遍历子行

UltraGridBand band = this.ultraGrid1.DisplayLayout.Bands[1];

foreach (UltraGridRow row in band.GetRowEnumerator(GridRowType.DataRow)) { }

遍历所有行,包括子band

foreach ( UltraGridRow row in this.ultraGrid1.Rows.GetRowEnumerator( GridRowType.DataRow, null, null ) ) { }

从保护单元格中获取值

private void ultraGrid1_DoubleClickCell(object sender, DoubleClickCellEventArgs e) {

Point point = Cursor.Position;

point = this.ultraGrid1.PointToClient(point);

UIElement oUI = this.ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(point);

if (oUI == null) return;

while (oUI != null) {

if (oUI.GetType() == typeof(CellUIElement))

{

CellUIElement oCellUI = (CellUIElement)oUI;

MessageBox.Show(\= \+ oCellUI.Cell.Value.ToString()); }

oUI = oUI.Parent; } }

获取指定坐标单元格内容

UIElement myUIElement = this.ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(new Point(e.X, e.Y));

UltraGridCell myCell = (UltraGridCell)myUIElement.GetContext(typeof(UltraGridCell));

MessageBox.Show(\are over a Cell containing \+ myCell.Value.ToString());

访问下拉框单元格控件

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

ValueList vlist=new ValueList(); for(int i=0; i<20; i++)

vlist.ValueListItems.Add(i, \\+ i);

e.Layout.Bands[0].Columns[0].ValueList=vlist;

e.Layout.Bands[0].Columns[0].style = ColumnStyle.DropDown;

EditorWithCombo editor = (EditorWithCombo)this.ultraGrid1.DisplayLayout.Bands

[0].Columns[0].Editor; editor.HasMRUList = true; editor.MaxMRUItems = 3;

editor.ButtonAlignment = ButtonAlignment.Left; } 剪贴板操作

Clipboard.SetDataObject(this.ultraGrid1.Selected.Cells);

this.ultraGrid1.DisplayLayout.Override.AllowMultiCellOperations =

AllowMultiCellOperation.Copy | AllowMultiCellOperation.Paste;

■■■■■■■■■■■■■■■■■■■■■■■■■

■■ CRUD ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■ 新增行 展现新增行 展现新增行

this.ultraGrid1.DisplayLayout.Override.AllowAddNew = AllowAddNew.TemplateOnTop; // TemplateOnBottom 提示文本设置

this.ultraGrid1.DisplayLayout.Override.TemplateAddRowPrompt = \ here to add rows...\

this.ultraGrid1.DisplayLayout.Bands[0].SpecialRowPromptField = \ss\

新增行样式设置

this.ultraGrid1.DisplayLayout.Override.TemplateAddRowCellAppearance.BackColor = Color.Yellow;

this.ultraGrid1.DisplayLayout.Override.TemplateAddRowCellAppearance.ForeColor = Color.LightYellow;

this.ultraGrid1.DisplayLayout.Override.TemplateAddRowSpacingBefore = 5;

this.ultraGrid1.DisplayLayout.Override.TemplateAddRowSpacingAfter = 5;

this.ultraGrid1.DisplayLayout.Override.BorderStyleTemplateAddRow = UIElementBorderStyle.Inset; 增加行:

this.ultraGrid1.Rows.Band.AddNew(); 新增行的值填充

this.ultraGrid1.DisplayLayout.Bands[0].Columns[\e = \

this.ultraGrid1.Rows[this.ultraGrid1.Rows.Count - 1].Cells[\ =

CommomFun.GetID(); 展现新增按钮

this.ultraGrid1.DisplayLayout.AddNewBox.Hidden = false;

private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) {

e.Layout.AddNewBox.Hidden = false; } 删除行: 删除选中的行

this.ultraGrid1.DeleteSelectedRows(false); 遍历删除选择行和激活行 bool blnDeleted = false;

for (int i = 0; i < this.ultraGrid1.Rows.Count; i++) {

if (this.ultraGrid1.Rows[i].Selected || this.ultraGrid1.Rows[i].IsActiveRow) {

if (this.ultraGrid1.Rows[i].Delete(false)) i--;

blnDeleted = true; } }

■■■■■■■■■■■■■■■■■■■■■■■■■ ■■ 数据校验 ■■ ■■■■■■■■■■■■■■■■■■■■■■■■■ 输入校验

对于无效输入:自动恢复为原始值

private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) {

// Set the property on the display layout's override so it affects the whole

WinGrid.

// You can override the property on a specific column.

e.Layout.Override.InvalidValueBehavior = InvalidValueBehavior.RevertValue;

e.Layout.Bands[0].Columns[0].InvalidValueBehavior = InvalidValueBehavior.RetainValueAndFocus; }

使用正则表达式限制输入

this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].RegexPattern = \-\\d{3}-\\d{4}\

private void ultraGrid1_Error(object sender, Infragistics.Win.UltraWinGrid.ErrorEventArgs e) {

e.Cancel = true;

if(e.ErrorType == ErrorType.Data)

MessageBox.Show(\is not a valid phone number!!\ }

捕捉输入错误

private void ultraGrid1_CellDataError(object sender, Infragistics.Win.UltraWinGrid.CellDataErrorEventArgs e) {

e.RaiseErrorEvent = false; // 阻止弹出错误提示窗口 e.RestoreOriginalValue = true; // 恢复原始值 e.StayInEditMode = true; // 继续保留在编辑模式 }

自定义删除提示框

private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e) {

e.DisplayPromptMsg = false;

e.Cancel = (DialogResult.Yes != MessageBox.Show(\确定删除该记录?\\删除\

MessageBoxButtons.YesNo)); }

自定义单元格数据错误提示框

private void grid_CellDataError(object sender, CellDataErrorEventArgs e) {

e.RaiseErrorEvent = false; e.StayInEditMode = true;

MessageBox.Show(\输入数据的格式不正确,请校验\ }

■■■■■■■■■■■■■■■■■■■■■■■■ ■■ 导出 ■■ ■■■■■■■■■■■■■■■■■■■■■■■■ 导出到Excel

(1)将 UltraGridExcelExporter 拖到窗体中

(2)ultraGridExcelExporter1.Export(ultraGrid1, \ (3)可附加控制导出Excel的细节

using Infragistics.Win.UltraWinGrid; // 修改worksheet名称

private void ultraGridExcelExporter1_BeginExport(object sender, ExcelExport.BeginExportEventArgs e) {

e.CurrentWorksheet = e.Workbook.Worksheets.Add(\Grid Data\

e.Workbook.ActiveWorksheet = e.CurrentWorksheet; }

// 阻止导出Owner字段

private void ultraGridExcelExporter1_CellExporting(object sender, ExcelExport.CellExportingEventArgs e) {

string sCellType = e.Value.GetType().FullName; if (sCellType == \.String\ {

string sCellContents = e.Value.ToString(); if (sCellContents.StartsWith(\

e.Value = \Confidential data *NOT* exported.\ } }

本文来源:https://www.bwwdw.com/article/xq85.html

Top