DATAGRID自定义控

原创 2004年08月13日 12:24:00

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing

Namespace CustomDataGrids

    <DefaultProperty("Text"), ToolboxData("<{0}:myGrid runat=server></{0}:myGrid>")> Public Class myGrid
        Inherits System.Web.UI.WebControls.DataGrid

#Region "Private Variables and Objects"
        Dim internalDataSet As DataSet                          'DataGrid Internal DataSet
        Dim internalDataView As DataView                        'DataGrid Internal DataView
        Dim internalTableName As String                         'Default Table Name (optional)
        Protected WithEvents textPage As New TextBox            'Define TextBox for current number
        Protected WithEvents labelError As New Label            'We want an error label in the footer
#End Region

#Region "Public Grid Properties "

        Public Property GridDataSet() As DataSet                'DataGrid DataSet
            Get
                Return internalDataSet
            End Get
            Set(ByVal Value As DataSet)
                internalDataSet = Value
            End Set
        End Property

        Public Property GridTable() As String                   'Default Table name (optional)
            Get
                Return internalTableName
            End Get
            Set(ByVal Value As String)
                internalTableName = Value
            End Set
        End Property

        Public ReadOnly Property ErrorMEssage() As String       'We want an error message
            Get
                Return Attributes("ErrorMessage")
            End Get
        End Property
#End Region

#Region "Grid Constructors"
        Public Sub New()
            'Set DataGrid Paging
            PagerStyle.Mode = PagerMode.NextPrev
            PagerStyle.ForeColor = Color.FromArgb(74, 60, 140)
            PagerStyle.BackColor = Color.FromArgb(231, 231, 255)
            Dim leftArrow As String = "<span style='font-family:webdings;font-size:medium;'>3</span>"
            Dim rightArrow As String = "<span style='font-family:webdings;font-size:medium;'>4</span>"
            PagerStyle.PrevPageText = leftArrow
            PagerStyle.NextPageText = rightArrow

            PagerStyle.PageButtonCount = 10
            PagerStyle.HorizontalAlign = HorizontalAlign.Right

            AllowPaging = True
            PageSize = 4

            'Visual Settings
            AutoGenerateColumns = False
            GridLines = GridLines.None
            CellSpacing = 0
            CellPadding = 3
            BorderColor = Color.FromArgb(231, 231, 255)
            BorderStyle = BorderStyle.Solid
            BorderWidth = Unit.Pixel(1)
            ForeColor = Color.Black
            Font.Size = FontUnit.XSmall
            Font.Name = "Verdana"

            'Settings for Normal Rows
            ItemStyle.ForeColor = Color.FromArgb(74, 60, 140)
            ItemStyle.BackColor = Color.FromArgb(231, 231, 255)

            'Settings for Alternating Rows
            AlternatingItemStyle.BackColor = Color.FromArgb(247, 247, 247)

            'Settings for Selected Rows
            SelectedItemStyle.ForeColor = Color.FromArgb(247, 247, 247)
            SelectedItemStyle.BackColor = Color.FromArgb(115, 138, 156)

            'Settings for Heading
            HeaderStyle.Font.Name = "Veranda"
            HeaderStyle.Font.Bold = True
            HeaderStyle.ForeColor = Color.FromArgb(247, 247, 247)
            HeaderStyle.BackColor = Color.FromArgb(74, 60, 140)
            HeaderStyle.HorizontalAlign = HorizontalAlign.Center

            'Settings for Footer
            ShowFooter = True
            FooterStyle.ForeColor = Color.FromArgb(74, 60, 140)
            FooterStyle.BackColor = Color.FromArgb(181, 199, 222)

            'Handle the OnPageIndexChanged Event and Item Created Event internally
            AddHandler ItemCreated, AddressOf OnItemCreated
            AddHandler PageIndexChanged, AddressOf OnPageIndexChanged

        End Sub

#End Region

#Region "Intercept ItemCreated and add features:  HEADER for Sorting --> up/down error, PAGER for custom Pageing, FOOTER for summary & Buttons"
        Public Shadows Sub OnItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
            Dim itemType As ListItemType = e.Item.ItemType      'What item whas created?

            If itemType = ListItemType.Footer Then              'FOOTER --> Erase custom footer and replace with three cells
                Dim footerRow As TableCellCollection = e.Item.Cells
                Dim footerCellCount As Integer = footerRow.Count

                Dim i As Integer                                'Remove existing Cells
                For i = footerCellCount - 1 To 0 Step -1
                    e.Item.Cells.RemoveAt(i)
                Next

                Dim newCell As New TableCell                    'Add just one cell
                newCell.ColumnSpan = footerCellCount
                newCell.HorizontalAlign = HorizontalAlign.Left

                Dim labelReturnRows As New Label
                Dim totalRows As Integer
                Try
                    labelReturnRows.Text = "This is Page " + CType(CurrentPageIndex + 1, String) + " of " + CType(PageCount, String) + " Pages."
                Catch
                End Try

                newCell.Controls.Add(labelReturnRows)
                e.Item.Cells.Add(newCell)
            End If

            If itemType = ListItemType.Pager Then                       'PAGER --> Add Page and Number to default behavior
                Dim cellPager As TableCell = e.Item.Cells(0)
                Dim cellControls As ControlCollection = cellPager.Controls
                Dim leftArrow As WebControl = CType(cellControls(0), WebControl)
                leftArrow.ToolTip = "Previous Record"
                Dim rightArrow As WebControl = CType(cellControls(2), WebControl)
                rightArrow.ToolTip = "Next Record"

                cellPager.HorizontalAlign = HorizontalAlign.Center      'Pager aligns horizontal

                cellPager.Controls.AddAt(1, New LiteralControl("Page:")) 'Leteral Controls are faster than labels

                textPage.ID = "textPageID"                              'Set the textBox for current and selected Page
                textPage.Width = Unit.Pixel(30)
                textPage.Text = CType((CurrentPageIndex + 1), String)
                textPage.ToolTip = "Shows Current Page & allows setting of page number"

                Dim buttonGoto As New Button                            'Set the Goto Page Button with events
                buttonGoto.Text = "Go"
                buttonGoto.ID = "buttonGoto"
                AddHandler buttonGoto.Click, AddressOf GotoPage_Click
                buttonGoto.ToolTip = "Click to goto selected Page"

                labelError.ID = "labelError"
                labelError.ForeColor = Color.Red
                labelError.Text = Attributes("ErrorMessage")

                cellPager.Controls.AddAt(2, textPage)
                cellPager.Controls.AddAt(3, New LiteralControl("&nbsp"))
                cellPager.Controls.AddAt(4, buttonGoto)
                cellPager.Controls.AddAt(7, labelError)

                cellPager.HorizontalAlign = HorizontalAlign.Center
            End If
        End Sub

#End Region

#Region "Custom Grid Event Handlers - PageIndexChanged"
        Public Shadows Sub OnPageIndexChanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
            CurrentPageIndex = e.NewPageIndex
            Attributes("ErrorMessage") = ""                             'Make sure the error is reset
            Update()
        End Sub
#End Region

#Region "OnGotoPage Event Handler"
        Private Sub GotoPage_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim newPage As Integer
            newPage = CInt(textPage.Text) - 1                           'Get new page from TextBox
            If newPage >= 0 And newPage <= PageCount - 1 Then           'Validate Page
                CurrentPageIndex = newPage
                Attributes("ErrorMessage") = ""                         'Reset the Error Message
                Update()
            Else
                Attributes("ErrorMessage") = "  Incorrect Page Entry"   'Error Message
                Update()
            End If
        End Sub
#End Region

#Region "Update() - Internal Data Update"
        Public Sub Update()
            'Even though the DefaultView would be used by the Grid, we might need to call it from a specific table in the Dataset
            Try
                If internalTableName = Nothing Then                             'Do we choose the named or default table?
                    internalDataView = internalDataSet.Tables(0).DefaultView
                Else
                    internalDataView = internalDataSet.Tables(internalTableName).DefaultView
                End If
                DataSource = internalDataView                                   'Update Internal DataView
                DataBind()
            Catch
                DataBind()  'Calling DataBind will avoid and error and return an empty grid
            End Try

        End Sub
#End Region


    End Class
End Namespace

datagrid自定义分页控件

  • qq_31971935
  • qq_31971935
  • 2016年08月10日 17:32
  • 489

DataGrid 自定义字段内容

因为DataGrid Web 控件的AutoGenerateColumn 属性预设为True,表示会自动产生数据源中所有的字段。如果我们想自订DataGrid Web 控制所要显示的字段,只要将Aut...
  • limshirley
  • limshirley
  • 2017年04月17日 14:16
  • 449

easyUI——DataGrid自定义操作列

项目中遇到一个问题:在easyUI的datagrid中增加一列为操作列。官网的demo中没有给出具体的解决问题的方法。解决的思路就是利用单元格的属性formatter中添加内容。...
  • suneqing
  • suneqing
  • 2015年01月31日 15:54
  • 5022

WPF DataGrid自定义样式

WPF DataGrid自定义样式 微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师)。 下面,找到我的小抄造型的网格。它不是100...
  • Metal1
  • Metal1
  • 2014年11月19日 22:43
  • 13094

easyui datagrid自定义操作

很多时候要在表格的最后添加一列操作列,easyUI貌似没有提供这种功能,不过没关系,我们可以自定义来实现 版本:jquery easyUI 1.3.2 这里我的实现方式是采用HTML形式,js方式...
  • Pompeii
  • Pompeii
  • 2014年11月19日 20:22
  • 902

easyui datagrid自定义表头

第一种实现方法: 实现界面: html代码: js代码: $('#tt').treegrid({ url: '/cost/getInfo.do', method: ...
  • tolcf
  • tolcf
  • 2015年09月10日 18:31
  • 4291

easyUI datagrid自定义添加列 (点击事件)

有列表 想添加一个操作列  类似删除之类的   ...
  • nnn_net
  • nnn_net
  • 2016年07月13日 15:42
  • 9782

含自定义渲染器或者编辑器的DataGrid的列焦点切换(Enter建)

DataGrid中列在可编辑状态下,使用Enter键快速切换焦点的使用方法,包含使用
  • vincent50411
  • vincent50411
  • 2014年08月18日 16:21
  • 392

datagrid数据格式解析-自定义过滤函数loadFilter

$('#tTable').datagrid({ fit:true, iconCls:'icon-liebiao', width:200, height:450, sing...
  • kunkkaCoco
  • kunkkaCoco
  • 2015年07月27日 15:37
  • 3321

EasyUI中,datagrid页码上的自定义按钮不显示问题

在分页Datagrid上,如果把定义分页上按钮的代码放在定义分页的JS代码前面,那么自定义的按钮就无法显示。...
  • wengengeng
  • wengengeng
  • 2015年02月08日 14:21
  • 1388
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:DATAGRID自定义控
举报原因:
原因补充:

(最多只允许输入30个字)