SuperGrid Basic(基础)
SuperGrid
Basic(基础)
具体的源码例子,请参考:C:\Users\Wene\Documents\DotNetBar WinForms Samples\SuperGrid
第一个表格为




:在表格列表头上显示一个Caption 支持HTML标记





在表结尾还可以显示一个Footer,支持HTML标记
ShowRowHeaders:是否显示行头
ShowRowGridIndex:是否在行头显示行索引
PurgeDeletedRows:清除被删除的行,默认删除的行会在FlatDeletedRows列表中,灰色显示或隐藏,这个方法彻底清除删除的行


控件相关属性,方法,事件
属性:BindingContext
获取列,及列相关属性和方法


'''
<summary>
''' GridButtonXEditControl Class that controls
the
''' ButtonX color initialization and user button
clicks.
'''
</summary>
Private Class
MyGridButtonXEditControl
Inherits
GridButtonXEditControl
'''
<summary>
'''
Constructor
'''
</summary>
Public Sub New()
' We want to be notified when the user clicks the
button
' so that we can change the underlying cell value to
reflect
' the mouse
click.
AddHandler Click, AddressOf
MyGridButtonXEditControlClick
End Sub
#Region "InitializeContext"
'''
<summary>
''' Initializes the color table for the
button
'''
</summary>
''' <param
name="cell"></param>
''' <param
name="style"></param>
Public OverridesSub InitializeContext(ByVal cell As
GridCell, ByVal style As
CellVisualStyle)
MyBase.InitializeContext(cell,
style)
Dim running AsBoolean = Text.Equals("Stop") = False
ColorTable
= IIf(running = True,
eButtonColor.OrangeWithBackground,
eButtonColor.BlueOrb)
End Sub
#End Region
#Region "MyGridButtonXEditControlClick"
'''
<summary>
''' Handles user clicks of the
button
'''
</summary>
''' <param
name="sender"></param>
''' <param
name="e"></param>
Private Sub
MyGridButtonXEditControlClick(ByVal sender
As Object,
ByVal e As
EventArgs)
Dim running AsBoolean = (EditorCell.Value IsNot Nothing AndAlso
EditorCell.Value.Equals("Start"))
EditorCell.Value
= IIf(running = True, "Stop", "Start")
End Sub
#End Region
End Class
还可以使用
EditorParams来传递自定义参数,如
panel.Columns("ComboBoxEx").EditorType = GetType(FragrantComboBox)
panel.Columns("ComboBoxEx").EditorType = GetType(FragrantComboBox)
panel.Columns("ComboBoxEx").EditorParams = New Object() {
orderArray }
Friend Class
FragrantComboBox
Inherits
GridComboBoxExEditControl
Public Sub New(ByVal
orderArray As
IEnumerable)
DataSource
= orderArray
End Sub
End Class
panel.Columns("ComboBoxEx").EditorType = GetType(FragrantComboBox)
panel.Columns("ComboBoxEx").EditorType = GetType(FragrantComboBox)
panel.Columns("ComboBoxEx").EditorParams = New Object() {
orderArray }
Friend Class
FragrantComboBox
Inherits
GridComboBoxExEditControl
Public Sub New(ByVal
orderArray As
IEnumerable)
DataSource
= orderArray
End Sub
End Class
EditControl,使用此属性来设定自定义事件
Dim ddc As
GridTextBoxDropDownEditControl = TryCast(panel.Columns("TextBoxDropDown").EditControl,
GridTextBoxDropDownEditControl)
If ddc IsNot Nothing Then
AddHandler ddc.ButtonClearClick, AddressOf
DdcButtonClearClick
End If
Dim ddc As
GridTextBoxDropDownEditControl = TryCast(panel.Columns("TextBoxDropDown").EditControl,
GridTextBoxDropDownEditControl)
If ddc IsNot Nothing Then
AddHandler ddc.ButtonClearClick, AddressOf
DdcButtonClearClick
End If
RenderType,可选,如果不设置,默认就是EditorType,如果设置,则可以在呈现时与编辑时不同的表示
获取行及行的相关属性和方法
方法:
.Rows 行可以通过添加Rows属性增加从属关系的子行
如图:
.Rows
行可以通过添加GridPanel增加行的从属子表
Dim panel As New
GridPanel()
InitPanel(panel)
crow.Rows.Add(panel)
Dim panel As New
GridPanel()
InitPanel(panel)
crow.Rows.Add(panel)
行的属性
row.InfoText
在行头显示此行的信息
如图:

在行头显示此行的信息

行的事件

AddHandler superGridControl1.CompareElements, AddressOf
SuperGridControl1CompareElements
'''
<summary>
''' Routine used to compare two IP
addresses
'''
</summary>
''' <param
name="sender"></param>
''' <param
name="e"></param>
Private Sub
SuperGridControl1CompareElements (ByVal sender AsObject, ByVal e As
GridCompareElementsEventArgs)
Dim cellA As
GridCell = TryCast(e.ElementA,
GridCell)
If cellA IsNot Nothing AndAlso
cellA.ColumnIndex = 0 Then
e.Cancel
= True
Dim cellB As
GridCell = CType(e.ElementB,
GridCell)
Dim valueA AsString = GetIpValue(TryCast(cellA.Value, String))
Dim valueB AsString = GetIpValue(TryCast(cellB.Value, String))
e.Result
= String.Compare(valueA,
valueB)
End If
End Sub
SuperGridControl1CompareElements
'''
<summary>
''' Routine used to compare two IP
addresses
'''
</summary>
''' <param
name="sender"></param>
''' <param
name="e"></param>
Private Sub
SuperGridControl1CompareElements (ByVal sender AsObject, ByVal e As
GridCompareElementsEventArgs)
Dim cellA As
GridCell = TryCast(e.ElementA,
GridCell)
If cellA IsNot Nothing AndAlso
cellA.ColumnIndex = 0 Then
e.Cancel
= True
Dim cellB As
GridCell = CType(e.ElementB,
GridCell)
Dim valueA AsString = GetIpValue(TryCast(cellA.Value, String))
Dim valueB AsString = GetIpValue(TryCast(cellB.Value, String))
e.Result
= String.Compare(valueA,
valueB)
End If
End Sub

可以通过此事件,来行编辑结束后,判断整行的数据合法性,如果不合法,可以通过设置e.Cancel=true来阻止提交

行通过验证


AddHandler
superGridControl1.RowSetDefaultValues, AddressOf
SuperGridControl1RowSetDefaultValues
superGridControl1.RowSetDefaultValues, AddressOf
SuperGridControl1RowSetDefaultValues
Cell单元格相关
事件:

If value < 0 OrElse value > 100Then
e.GridCell.InfoText
= "Cell Value must be between 0 and 100,
inclusive"
e.Cancel
= True
End If




AddHandler superGridControl1.CellValueChanged,
AddressOf
SuperGridControl1CellValueChanged
编辑
重新刷新某一个单元格,使得单元格的呈现方式变化
cell.GridRow.Cells(3).InvalidateRender()
'
响应事件GetRenderer,就可以自定义呈现的方式
Private Sub
SuperGridControl1GetRenderer(ByVal sender
As Object,
ByVal e As
GridGetRendererEventArgs) Handles
superGridControl1.GetRenderer
Dim columnIndex AsInteger =
e.GridCell.ColumnIndex
' Just as an example, lets have all CheckBox cells,
whose
' adjacent SwitchButton cell is 'true', to render as a
textual
' value, instead of the normal checkbox
display.
If columnIndex = 3 Then
Dim scell As
GridCell = e.GridCell.GridRow.Cells(2)
e.RenderType
= IIf(CBool(scell.Value) = True, GetType(GridTextBoxXEditControl), Nothing)
End If
End Sub
cell.GridRow.Cells(3).InvalidateRender()
'
响应事件GetRenderer,就可以自定义呈现的方式
Private Sub
SuperGridControl1GetRenderer(ByVal sender
As Object,
ByVal e As
GridGetRendererEventArgs) Handles
superGridControl1.GetRenderer
Dim columnIndex AsInteger =
e.GridCell.ColumnIndex
' Just as an example, lets have all CheckBox cells,
whose
' adjacent SwitchButton cell is 'true', to render as a
textual
' value, instead of the normal checkbox
display.
If columnIndex = 3 Then
Dim scell As
GridCell = e.GridCell.GridRow.Cells(2)
e.RenderType
= IIf(CBool(scell.Value) = True, GetType(GridTextBoxXEditControl), Nothing)
End If
End Sub
点击了单元格中的链接,引发MarkupLinkClick事件
示例:指定单元格的控件为如下类,即可实现点击链接,打开相应的页面,也可以执行相应的操作
Private Class
MyGridLabelXEditControl
Inherits
GridLabelXEditControl
Public Sub New()
AddHandler MarkupLinkClick, AddressOf
MyGridLabelXEditControlMarkupLinkClick
End Sub
#Region "MyGridLabelXEditControlMarkupLinkClick"
Private Sub
MyGridLabelXEditControlMarkupLinkClick(ByVal
sender As Object, ByVal e
As
MarkupLinkClickEventArgs)
Dim sInfo As New
ProcessStartInfo(e.HRef)
Process.Start(sInfo)
End Sub
#End Region
End Class
Private Class
MyGridLabelXEditControl
Inherits
GridLabelXEditControl
Public Sub New()
AddHandler MarkupLinkClick, AddressOf
MyGridLabelXEditControlMarkupLinkClick
End Sub
#Region "MyGridLabelXEditControlMarkupLinkClick"
Private Sub
MyGridLabelXEditControlMarkupLinkClick(ByVal
sender As Object, ByVal e
As
MarkupLinkClickEventArgs)
Dim sInfo As New
ProcessStartInfo(e.HRef)
Process.Start(sInfo)
End Sub
#End Region
End Class
处理大量的数据时可以用
superGridControl1.BeginUpdate()
For Each row As GridContainer In
panel.FlatDeletedRows
row.Visible
= False
Next
row
superGridControl1.EndUpdate()
superGridControl1.BeginUpdate()
For Each row As GridContainer In
panel.FlatDeletedRows
row.Visible
= False
Next
row
superGridControl1.EndUpdate()
专题
数据绑定

BindingManagerBase
- 通过BindingManagerBase的Position属性,可以明确当前定位行,
-
BindingManagerBase.RemoveAt方法,可以删除指定行
-
Count属性返回总记录数
-
事件PositionChanged
可以响应行变化事件
context的FirstName字段,绑定后,会自动更新

SortableBindingList
或 BindingList(Of T)
或者 实现 IListSource的数据类

如果使用List(of T)作为数据源,在数据源变动,被删除或新增后,不会自动更新界面进行显示,需要手动调用
如果使用List(of
T)作为数据源,不会自动通知绑定同一个数据源的其他控件
使用自定义控件
自已写的一个例子,可以显示时间字段时,格式化为长时间显示
Dim panel As GridPanel = xDataGridView1.PrimaryGrid
Dim column As GridColumn = panel.Columns(“xColumnTime”)
column.EditorType = GetType(MyTimeLableXEditControl)
Friend Class
MyTimeLableXEditControl
Inherits
GridLabelXEditControl
Public OverridesSub CellRender(g As
System.Drawing.Graphics)
Dim r As Rectangle
=
EditorCell.Bounds
Dim pString AsString = String.Empty
If (EditorCell.Value IsNot Nothing) Then
Dim pValue As
DateTime = CType(EditorCell.Value,
DateTime)
pString
=
pValue.ToLongTimeString()
'pString = pValue.ToString("d日 HH:mm:ss")
End If
Using
br As Brush = New SolidBrush(EditorCell.GridRow.CellStyles.Default.TextColor)
g.DrawString(pString,
Font, br,
r)
End
Using
End Sub
End Class
MyTimeLableXEditControl
Inherits
GridLabelXEditControl
Public OverridesSub CellRender(g As
System.Drawing.Graphics)
Dim r As Rectangle
=
EditorCell.Bounds
Dim pString AsString = String.Empty
If (EditorCell.Value IsNot Nothing) Then
Dim pValue As
DateTime = CType(EditorCell.Value,
DateTime)
pString
=
pValue.ToLongTimeString()
Using
br As Brush = New SolidBrush(EditorCell.GridRow.CellStyles.Default.TextColor)
g.DrawString(pString,
Font, br,
r)
End
Using
End Sub
End Class